Delay resolution until after application services are built (#1553)

- Resolve the logger from the right service provider to log duplicate hosting startup assemblies.
- Don't create a 3rd IServiceProvider if we resolved the default implementation.
This commit is contained in:
David Fowler 2018-09-24 10:41:30 -07:00 committed by GitHub
parent ef69aa833b
commit f17142b2ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 8 deletions

View File

@ -174,13 +174,6 @@ namespace Microsoft.AspNetCore.Hosting
}
}
var logger = hostingServiceProvider.GetRequiredService<ILogger<WebHost>>();
// Warn about duplicate HostingStartupAssemblies
foreach (var assemblyName in _options.GetFinalHostingStartupAssemblies().GroupBy(a => a, StringComparer.OrdinalIgnoreCase).Where(g => g.Count() > 1))
{
logger.LogWarning($"The assembly {assemblyName} was specified multiple times. Hosting startup assemblies should only be specified once.");
}
AddApplicationServices(applicationServices, hostingServiceProvider);
var host = new WebHost(
@ -193,6 +186,14 @@ namespace Microsoft.AspNetCore.Hosting
{
host.Initialize();
var logger = host.Services.GetRequiredService<ILogger<WebHost>>();
// Warn about duplicate HostingStartupAssemblies
foreach (var assemblyName in _options.GetFinalHostingStartupAssemblies().GroupBy(a => a, StringComparer.OrdinalIgnoreCase).Where(g => g.Count() > 1))
{
logger.LogWarning($"The assembly {assemblyName} was specified multiple times. Hosting startup assemblies should only be specified once.");
}
return host;
}
catch
@ -208,7 +209,7 @@ namespace Microsoft.AspNetCore.Hosting
var provider = collection.BuildServiceProvider();
var factory = provider.GetService<IServiceProviderFactory<IServiceCollection>>();
if (factory != null)
if (factory != null && !(factory is DefaultServiceProviderFactory))
{
using (provider)
{