title: ASP.NET CORE 2.2 Deployment Issue
body: <p>I'm having the weirdest issue and I have no idea why.
  When deploying our .net core 2.2 api to our local IIS server I get the following error message:</p>
  
  <blockquote>
    <p>HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure</p>
  </blockquote>
  
  <p>After checking the event log I this is the error that I find:</p>
  
  <pre><code>Application: dotnet.exe
  CoreCLR Version: 4.6.27207.3
  Description: The process was terminated due to an unhandled exception.
  Exception Info: System.InvalidOperationException: No service for type 'Digitise.Infrastructure.Services.DatabaseMigrator' has been registered.
     at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
     at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
     at Digitise.ApiBase.BaseProgram.Initialise(String[] args, IWebHost host) in C:\Projects\Digitise.AspNetCore\Digitise.ApiBase\BaseProgram.cs:line 17
     at Digitise.Api.Program.Main(String[] args) in C:\Projects\Digitise.AspNetCore\Digitise.Api\Program.cs:line 27
  </code></pre>
  
  <p>It seems like the DI is not working correctly! The weird thing is if I run the <code>api.exe</code> or <code>dotnet api.dll</code> the API works perfectly :/</p>
  
  <p>Anyone have any ideas? :)</p>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>public class Program    
      {
              public static object _lock = new object();
              public static bool _init = false;
  
              public static void Main(string[] args)
              {
                  var host = CreateWebHostBuilder(args);
                  Initialise(args, host);
              }
  
              public static IWebHost CreateWebHostBuilder(string[] args) =&gt;
                  WebHost.CreateDefaultBuilder(args)
                      .UseStartup&lt;Startup&gt;()
                      //.UseIISIntegration()
                      .UseIIS()
                      .UseNLog()
                      .UseShutdownTimeout(TimeSpan.FromSeconds(10))
                      .Build();
  
              public static void Initialise(string[] args, IWebHost host)
              {
                  var logger = NLogBuilder.ConfigureNLog(Path.Combine(Directory.GetCurrentDirectory(), "NLog.config")).GetCurrentClassLogger();
                  try
                  {
                      logger.Debug("App init");
  
                      using (var scope = host.Services.CreateScope())
                      {
                          var services = scope.ServiceProvider;
                          try
                          {
                              if (!_init)
                              {
                                  lock (_lock)
                                  {
                                      if (!_init)
                                      {
                                          lock (_lock)
                                          {
                                              services.GetRequiredService&lt;DatabaseMigrator&gt;().Migrate();
                                          }
                                      }
                                  }
                              }
                          }
                          catch (Exception ex)
                          {
                              logger.Error(ex, "An error occurred while starting up the app.");
                              throw;
                          }
                      }
  
                      host.Run();
                  }
                  catch (Exception e)
                  {
                      logger.Error(e, "Stopped app due to exception");
                      throw;
                  }
              }
          }
  </code></pre>
  
  <p><strong>DatabaseMigrator.cs</strong></p>
  
  <pre><code>public class DatabaseMigrator
      {
          private readonly TenantDbContext _tenantDbContext;
          private readonly IOptions&lt;DatabaseConfiguration&gt; _databaseConfig;
          private readonly ILogger&lt;DatabaseMigrator&gt; _logger;
          private readonly AdminTenantDbInitialiser _adminTenantDbInitialiser;
  
          public DatabaseMigrator(TenantDbContext tenantDbContext, IOptions&lt;DatabaseConfiguration&gt; databaseConfig, ILogger&lt;DatabaseMigrator&gt; logger, AdminTenantDbInitialiser adminTenantDbInitialiser)
          {
              _tenantDbContext = tenantDbContext;
              _databaseConfig = databaseConfig;
              _logger = logger;
              _adminTenantDbInitialiser = adminTenantDbInitialiser;
          }
  
          public void Migrate()
          {
             //migration logic
          }
      }
  </code></pre>