title: How to override services that mocked using IWebHostBuilder.ConfigureTestServices in some tests
body: <p>I have a custom <code>WebApplicationFactory&lt;Startup&gt;</code> for that i mocked some services to use for integration testing, like <code>DatabaseSeeder</code> in order to make the tests run faster.</p>
  
  <p>Now i want to test the database seeding so I want to override the configuration to use the real database seeder this test.</p>
  
  <p>I tried using <code>WithWebHostBuilder</code>, it returns a new <code>WebApplicationFactory&lt;Startup&gt;</code> but the <code>builder.ConfigureTestServices</code> method in this new factory run before the original so it get override by the original factory <code>ConfigureTestServices</code></p>
  
  <pre><code>  /// &lt;summary&gt;
      /// custom web application factory for testing.
      /// &lt;/summary&gt;
      public class LwtTestWebApplicationFactory : WebApplicationFactory&lt;Startup&gt;
      {
          ....
  
          /// &lt;inheritdoc /&gt;
          protected override void ConfigureWebHost(IWebHostBuilder builder)
          {
              builder.ConfigureTestServices(
                  services =&gt;
                  {
                      // Replace database seeder.
                      ServiceDescriptor databaseSeederDescriptor =
                          services.Single(s =&gt; s.ServiceType == typeof(IDatabaseSeeder));
  
                      services.Remove(databaseSeederDescriptor);
  
                      var mock = new Mock&lt;IDatabaseSeeder&gt;();
                      mock.Setup(s =&gt; s.SeedData())
                          .Returns(Task.CompletedTask);
                      services.AddTransient(resolver =&gt; mock.Object);
  
                      // Replace index creator.
                      ServiceDescriptor indexCreatorDescriptor =
                          services.Single(s =&gt; s.ServiceType == typeof(IIndexCreator));
  
                      services.Remove(indexCreatorDescriptor);
  
                      var indexCreatorMock = new Mock&lt;IIndexCreator&gt;();
                      indexCreatorMock.Setup(s =&gt; s.CreateIndexesAsync())
                          .Returns(Task.CompletedTask);
                      services.AddTransient(resolver =&gt; indexCreatorMock.Object);
  
                      // Remove the app's ApplicationDbContext registration.
                      ServiceDescriptor descriptor = services.SingleOrDefault(
                          d =&gt; d.ServiceType == typeof(DbContextOptions&lt;IdentityDbContext&gt;));
  
                      if (descriptor != null)
                      {
                          services.Remove(descriptor);
                      }
  
                      // Add ApplicationDbContext using an in-memory database for testing.
                      services.AddDbContext&lt;IdentityDbContext&gt;(
                          (options, context) =&gt; { context.UseInMemoryDatabase("InMemoryDbForTesting"); });
  
                      ServiceDescriptor appSettingDescriptor = services.SingleOrDefault(
                          d =&gt; d.ServiceType == typeof(AppSettings));
  
                      if (appSettingDescriptor != null)
                      {
                          var appSetting = (AppSettings)appSettingDescriptor.ImplementationInstance;
                          services.Remove(appSettingDescriptor);
                          services.AddSingleton(
                              new AppSettings
                              {
                                  MongoDatabase = appSetting.MongoDatabase,
                                  Secret = appSetting.Secret,
                                  MongoConnectionString = this.mongoDbRunner.ConnectionString,
                              });
                      }
                  });
          }
  
          .....
      }
  }
  </code></pre>
  
  <p>What i tried.</p>
  
  <pre><code>    using (var factory = new LwtTestWebApplicationFactory())
      using (WebApplicationFactory&lt;Startup&gt; seedEnabledFactory = factory.WithWebHostBuilder(
                  builder =&gt;
                  {
                      builder.ConfigureTestServices(
                          services =&gt;
                          {
                              // Replace database seeder.
                              ServiceDescriptor databaseSeederDescriptor =
                                  services.Single(s =&gt; s.ServiceType == typeof(IDatabaseSeeder));
  
                              services.Remove(databaseSeederDescriptor);
  
                              services.AddTransient&lt;IDatabaseSeeder, DatabaseSeeder&gt;();
  
                              // Replace index creator.
                              ServiceDescriptor indexCreatorDescriptor =
                                  services.Single(s =&gt; s.ServiceType == typeof(IIndexCreator));
  
                              services.Remove(indexCreatorDescriptor);
  
                              services.AddTransient&lt;IIndexCreator, MongoDbIndexCreator&gt;();
                          });
                  }))
  </code></pre>