title: Error WebHostBuilder allows creation only of a single instance of WebHost
body: <p>Upgraded from .NET Core 2.0 to .NET Core 2.1</p>
  
  <p>ERROR
  WebHostBuilder allows creation only of a single instance of WebHost  </p>
  
  <hr>
  
  <p>Program.cs</p>
  
  <pre><code>namespace WebApp1
  {
      public class Program
      {
          public static void Main(string[] args)
          {
  
              CreateWebHostBuilder(args)
                  .MigrateDbContext&lt;ApplicationDbContext&gt;((context, services) =&gt;
                  {
                      var env = services.GetService&lt;IHostingEnvironment&gt;();
                      var logger = services.GetService&lt;ILogger&lt;ApplicationDbContextSeed&gt;&gt;();
  
                      new ApplicationDbContextSeed()
                          .SeedAsync(context, env, logger)
                          .Wait();
                  }).Build().Run();
          }
          public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;
              WebHost.CreateDefaultBuilder(args)
                  .UseContentRoot(Directory.GetCurrentDirectory())
                  .UseStartup&lt;Startup&gt;()
                  .ConfigureAppConfiguration((builderContext, config) =&gt;
                  {
                      config.AddEnvironmentVariables();
                  });
      }
  }
  </code></pre>
  
  <p>IWebHostExtensions.cs</p>
  
  <pre><code>using Microsoft.EntityFrameworkCore;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Logging;
  using System;
  
  namespace Microsoft.AspNetCore.Hosting
  {
      public static class IWebHostExtensions
      {
          public static IWebHostBuilder MigrateDbContext&lt;TContext&gt;(this IWebHostBuilder webHost, Action&lt;TContext, IServiceProvider&gt; seeder) where TContext : DbContext
          {
              using (var scope = webHost.Build().Services.CreateScope())
              {
                  var services = scope.ServiceProvider;
  
                  var logger = services.GetRequiredService&lt;ILogger&lt;TContext&gt;&gt;();
  
                  var context = services.GetService&lt;TContext&gt;();
  
                  try
                  {
                      logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}");
  
                      context.Database
                          .Migrate();
  
                      seeder(context, services);
  
                      logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}");
                  }
                  catch (Exception ex)
                  {
                      logger.LogError(ex, $"An error occurred while migrating the database used on context {typeof(TContext).Name}");
                  }
              }
  
              return webHost;
          }
      }
  }
  </code></pre>
  
  <p>Credit: </p>
  
  <p><a href="https://github.com/dotnet-architecture/eShopOnContainers/blob/c83c6842f593aaaae59adefa78a412a4a6c87faa/src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs" rel="nofollow noreferrer">https://github.com/dotnet-architecture/eShopOnContainers/blob/c83c6842f593aaaae59adefa78a412a4a6c87faa/src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs</a></p>
  
  <p><a href="https://github.com/dotnet-architecture/eShopOnContainers/blob/a9676b4b5667eeebe0aa8ff5f64c604e9bd3971a/src/Services/Identity/Identity.API/Program.cs" rel="nofollow noreferrer">https://github.com/dotnet-architecture/eShopOnContainers/blob/a9676b4b5667eeebe0aa8ff5f64c604e9bd3971a/src/Services/Identity/Identity.API/Program.cs</a></p>
  
  <p><a href="https://github.com/dotnet-architecture/eShopOnContainers/blob/a9676b4b5667eeebe0aa8ff5f64c604e9bd3971a/src/Services/Identity/Identity.API/Data/ApplicationDbContextSeed.cs" rel="nofollow noreferrer">https://github.com/dotnet-architecture/eShopOnContainers/blob/a9676b4b5667eeebe0aa8ff5f64c604e9bd3971a/src/Services/Identity/Identity.API/Data/ApplicationDbContextSeed.cs</a></p>