title: How do I get a instance of a service in ASP.NET Core 3.1
body: <p>I have a small project in .NET Core 2.0 and as Microsoft announced that would no longer support .NET Core 2.0 I tried to update the project to the latest version at this time is 3.1. But I had a hard time configuring the Dependency Injection and need some help.</p>
  
  <p>In order to populate the database, I need to get required services such as Db Context and user configuration and pass this on to DbInitialize class as shown below. I did this in Program.cs before Startup.cs configuration.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Program {
          public static void Main(string[] args)
          {
  
              var host = BuildWebHost(args);
  
              using (var scope = host.Services.CreateScope())
              {
                  var services = scope.ServiceProvider;
                  try
                  {
                      var context = services.GetRequiredService&lt;GameStoreContext&gt;();
                      var configuration = services.GetRequiredService&lt;IConfiguration&gt;();
                      var userManager = services.GetRequiredService&lt;UserManager&lt;IdentityUser&gt;&gt;();
                      var roleManager = services.GetRequiredService&lt;RoleManager&lt;IdentityRole&gt;&gt;();
                      DbInitializer.Initialize(context, configuration, userManager,roleManager).GetAwaiter().GetResult();
                  }
                  catch (Exception ex)
                  {
                      var logger = services.GetRequiredService&lt;ILogger&lt;Program&gt;&gt;();
                      logger.LogError(ex, "An error occurred while seeding the database.");
                  }
              }
              host.Run();
          }
  
          public static IWebHost BuildWebHost(string[] args) =&gt;
              WebHost.CreateDefaultBuilder(args)
                  .UseStartup&lt;Startup&gt;()
                  .UseContentRoot(Directory.GetCurrentDirectory())
                  .Build();
      }
  </code></pre>
  
  <p>But in the .NET Core 3.1 version BuildWebHost can no longer be instantiated so I was unable to retrieve services like this.</p>
  
  <p>The new version of Program.cs in .NET Core 3.1 looks like this </p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Program
  {
      public static void Main(string[] args)
      {
          CreateHostBuilder(args).Build().Run();
      }
  
      public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
          Host.CreateDefaultBuilder(args)
              .ConfigureWebHostDefaults(webBuilder =&gt;
              {
                  webBuilder.UseStartup&lt;Startup&gt;();
              });
  }
  </code></pre>
  
  <p>There is any way that I can achieve the same result in the new Framework?</p>
  
  <p>OBS: I have read in some posts people advising to use IConfiguration but I couldn't find any example.</p>