diff --git a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs index 197eee9ba2..416df78198 100644 --- a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs +++ b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs @@ -159,15 +159,8 @@ namespace Microsoft.AspNetCore.Hosting var hostingServices = BuildHostingServices(); var hostingContainer = hostingServices.BuildServiceProvider(); - var appEnvironment = hostingContainer.GetRequiredService(); var startupLoader = hostingContainer.GetRequiredService(); - var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, appEnvironment.ApplicationBasePath); - var applicationName = ResolveApplicationName() ?? appEnvironment.ApplicationName; - - // Initialize the hosting environment - _hostingEnvironment.Initialize(applicationName, contentRootPath, _options); - var host = new WebHost(hostingServices, startupLoader, _options, _config); // Only one of these should be set, but they are used in priority @@ -184,6 +177,14 @@ namespace Microsoft.AspNetCore.Hosting { _options = new WebHostOptions(_config); + var defaultPlatformServices = PlatformServices.Default; + var appEnvironment = defaultPlatformServices.Application; + var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, appEnvironment.ApplicationBasePath); + var applicationName = ResolveApplicationName() ?? appEnvironment.ApplicationName; + + // Initialize the hosting environment + _hostingEnvironment.Initialize(applicationName, contentRootPath, _options); + var services = new ServiceCollection(); services.AddSingleton(_hostingEnvironment); @@ -218,8 +219,6 @@ namespace Microsoft.AspNetCore.Hosting // Ensure object pooling is available everywhere. services.AddSingleton(); - var defaultPlatformServices = PlatformServices.Default; - services.AddSingleton(defaultPlatformServices.Application); services.AddSingleton(defaultPlatformServices.Runtime);