相关疑难解决方法(0)

ASP.Net 6自定义WebApplicationFactory抛出异常

我正在将现有的 ASP.Net 5 Web 应用程序迁移到 ASP.Net 6,并遇到了通过集成测试的最后障碍。

\n

我自定义 WebApplicationFactory 并抛出异常:Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.

\n
    public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class\n    {\n        protected override void ConfigureWebHost(IWebHostBuilder builder)\n        {\n            Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "IntegrationTests");\n            builder.ConfigureServices(services => {\n                // Create a new service provider.\n                var serviceProvider = new ServiceCollection()\n                    .AddEntityFrameworkInMemoryDatabase().AddLogging()\n                    .BuildServiceProvider();\n\n                // Add a database context (AppDbContext) using an in-memory database for testing.\n                services.AddDbContextPool<AppDbContext>(options =>\n                {\n                    options.UseInMemoryDatabase("InMemoryAppDb");\n                    options.UseInternalServiceProvider(serviceProvider);\n                    options.EnableSensitiveDataLogging();\n                    options.EnableDetailedErrors();\n                    options.LogTo(Console.WriteLine);\n                });\n\n                services.AddDbContextPool<AppIdentityDbContext>(options …
Run Code Online (Sandbox Code Playgroud)

c# asp.net integration-testing asp.net-web-api asp.net-core

10
推荐指数
2
解决办法
9202
查看次数