Asp.net核心RC1到RC2的集成测试:UseServices去哪里了?

时间:2021-03-19 20:23:56

Related but not the same as this RC1 to RC2 integration test question, I am trying to figure out what happened to UseServices

相关但与RC1到RC2集成测试问题不一样,我试图弄清楚UseServices发生了什么

//RC1: 
// TestServer = new TestServer(TestServer
//                .CreateBuilder()
//                 .UseStartup<Startup>());
//RC2: 
// TestServer=new TestServer(new WebHostBuilder().UseEnvironment....)

var builder2 = new WebHostBuilder()
 .UseEnvironment("Development")
/* RC2 port:
.UseServices(services =>
{
                // Change the application environment to the mvc project
                var env = new                TestApplicationEnvironment();
    env.ApplicationBasePath = Path.GetFullPath(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "..", "..", "src", "RamSoft.Fhir.Api"));
    env.ApplicationName = "RamSoft Patient Api Service";

    services.AddInstance<IApplicationEnvironment>(env);
})
*/
.UseStartup<RamSoft.Fhir.Api.Startup>();
TestServer = new TestServer(builder2);
Client = TestServer.CreateClient();

The TestApplicationEnvironment in my sample above appears to be a minimal mock object, like this:

上面示例中的TestApplicationEnvironment似乎是一个最小的模拟对象,如下所示:

 public class TestApplicationEnvironment : IApplicationEnvironment
    {
        public string ApplicationBasePath { get; set; }

        public string ApplicationName { get; set; }

        public string ApplicationVersion => PlatformServices.Default.Application.ApplicationVersion;

        public string Configuration => PlatformServices.Default.Application.Configuration;

        public FrameworkName RuntimeFramework => PlatformServices.Default.Application.RuntimeFramework;

        public object GetData(string name)
        {
            return PlatformServices.Default.Application.GetData(name);
        }

        public void SetData(string name, object value)
        {
            PlatformServices.Default.Application.SetData(name, value);
        }
    }

There is no documentation (docs.asp.net has not been updated yet) on this. Does anyone know how to make this code compile? The missing code shown above commented out would have set up some application level environment settings and provided it as an Application Environment. So far it seems this is gone, and may be replaced by IHostingEnvironment, but how to provide the mocked IHostingEnvironment to an integration test is of course not clear, with the decoupled nature of things, this may be provided by some mixin that is not present on my project.json, and so I have no idea how to proceed.

此处没有文档(docs.asp.net尚未更新)。有谁知道如何编译这段代码?上面显示的缺失代码将被设置为一些应用程序级环境设置,并将其作为应用程序环境提供。到目前为止它似乎已经消失了,可能会被IHostingEnvironment取代,但是如何将模拟的IHostingEnvironment提供给集成测试当然不清楚,事物的解耦性质,这可能是由一些不存在的mixin提供的在我的project.json上,所以我不知道如何继续。

It may be that this code is no longer necessary for the test to pass, and if I figure out the solution, I will answer this myself.

这个代码可能不再是测试通过所必需的,如果我找到解决方案,我会自己回答。

At runtime with the code above commented out, I get an error that indicates that something required for the code to continue is missing, probably an issue with dependency inversion container not being able to resolve IConfiguration:

在上面的代码注释掉的运行时,我得到一个错误,表明代码继续所需的东西丢失,可能是依赖转换容器无法解析IConfiguration的问题:

System.Exception was unhandled by user code
  HResult=-2146233088
  Message=Could not resolve a service of type 'Microsoft.Extensions.Configuration.IConfiguration' for the parameter 'configuration' of method 'Configure' on type 'X.Y.Api.Startup'.
  Source=Microsoft.AspNetCore.Hosting
  StackTrace:
       at Microsoft.AspNetCore.Hosting.Startup.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
       at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
       at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
       at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder)
       at X.Y.IntegrationTests.TestServerFixture..ctor() in D:\....IntegrationTests\TestServerFixture.cs:line 68
  InnerException...

1 个解决方案

#1


1  

I think you're looking for ConfigureServices:

我想你正在寻找ConfigureServices:

.ConfigureServices(services => {
    //Make your `env`...
    services.AddSingleton(env);
});

#1


1  

I think you're looking for ConfigureServices:

我想你正在寻找ConfigureServices:

.ConfigureServices(services => {
    //Make your `env`...
    services.AddSingleton(env);
});