title: ASP.Net 6 custom WebApplicationFactory throws exception
body: <p>I am migrating my existing ASP.Net 5 web app to ASP.Net 6 and bump into the final hurdles of getting the integration tests to pass.</p>
  <p>I customize WebApplicationFactory and it throws exception: <code>Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.</code></p>
  <pre><code>    public class CustomWebApplicationFactory&lt;TStartup&gt; : WebApplicationFactory&lt;TStartup&gt; where TStartup : class
      {
          protected override void ConfigureWebHost(IWebHostBuilder builder)
          {
              Environment.SetEnvironmentVariable(&quot;ASPNETCORE_ENVIRONMENT&quot;, &quot;IntegrationTests&quot;);
              builder.ConfigureServices(services =&gt; {
                  // Create a new service provider.
                  var serviceProvider = new ServiceCollection()
                      .AddEntityFrameworkInMemoryDatabase().AddLogging()
                      .BuildServiceProvider();
  
                  // Add a database context (AppDbContext) using an in-memory database for testing.
                  services.AddDbContextPool&lt;AppDbContext&gt;(options =&gt;
                  {
                      options.UseInMemoryDatabase(&quot;InMemoryAppDb&quot;);
                      options.UseInternalServiceProvider(serviceProvider);
                      options.EnableSensitiveDataLogging();
                      options.EnableDetailedErrors();
                      options.LogTo(Console.WriteLine);
                  });
  
                  services.AddDbContextPool&lt;AppIdentityDbContext&gt;(options =&gt;
                  {
                      options.UseInMemoryDatabase(&quot;InMemoryIdentityDb&quot;);
                      options.UseInternalServiceProvider(serviceProvider);
                      options.EnableSensitiveDataLogging();
                      options.EnableDetailedErrors();
                      options.LogTo(Console.WriteLine);
                  });
                  services.AddScoped&lt;SignInManager&lt;AppUser&gt;&gt;();
                  services.AddScoped&lt;ILogger&lt;UserRepository&gt;&gt;(provider =&gt; {
                      ILoggerFactory loggerFactory = provider.GetRequiredService&lt;ILoggerFactory&gt;();
                      return loggerFactory.CreateLogger&lt;UserRepository&gt;();
                  });
                  services.AddDistributedMemoryCache();
                  // Build the service provider.
                  var sp = services.BuildServiceProvider();
  
                  // Create a scope to obtain a reference to the database contexts
                  using (var scope = sp.CreateScope())
                  {
                      var scopedServices = scope.ServiceProvider;
                      var appDb = scopedServices.GetRequiredService&lt;AppDbContext&gt;();
                      var identityDb = scopedServices.GetRequiredService&lt;AppIdentityDbContext&gt;();
                      var logger = scopedServices.GetRequiredService&lt;ILogger&lt;CustomWebApplicationFactory&lt;TStartup&gt;&gt;&gt;();
  
                      // Ensure the database is created.
                      appDb.Database.EnsureCreated();
                      identityDb.Database.EnsureCreated();
  
                      try
                      {
                          // Seed the database with test data.
                          SeedData.PopulateTestData(identityDb);
                          SeedData.PopulateTestData(appDb);
                      }
                      catch (Exception ex)
                      {
                          logger.LogError(ex, $&quot;An error occurred seeding the &quot; +
                              $&quot;database with test messages. Error: {ex.Message}&quot;);
                      }
                  }
              });
          }
      }
  </code></pre>
  <p>Exception:</p>
  <pre><code>  Message: 
      System.NotSupportedException : The content root changed from &quot;C:\Projects\C#\AspNetCoreApi\src\Web.Api\&quot; to &quot;C:\Projects\C#\AspNetCoreApi\test\Web.Api.IntegrationTests\bin\Debug\net6.0\&quot;. Changing the host configuration using WebApplicationBuilder.WebHost is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.
  
    Stack Trace: 
      ConfigureWebHostBuilder.UseSetting(String key, String value)
      HostingAbstractionsWebHostBuilderExtensions.UseContentRoot(IWebHostBuilder hostBuilder, String contentRoot)
      Program.&lt;Main&gt;$(String[] args) line 58
      --- End of stack trace from previous location ---
      HostingListener.CreateHost()
      &lt;&gt;c__DisplayClass8_0.&lt;ResolveHostFactory&gt;b__0(String[] args)
      DeferredHostBuilder.Build()
      WebApplicationFactory`1.CreateHost(IHostBuilder builder)
      WebApplicationFactory`1.ConfigureHostBuilder(IHostBuilder hostBuilder)
      WebApplicationFactory`1.EnsureServer()
      WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
      WebApplicationFactory`1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers)
      WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
      WebApplicationFactory`1.CreateClient()
      MyControllerIntegrationTests.ctor(CustomWebApplicationFactory`1 factory) line 15
  </code></pre>
  <p>Any advice and insight is appreciated.</p>