Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit f17142b

Browse files
authored
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.
1 parent ef69aa8 commit f17142b

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,6 @@ public IWebHost Build()
174174
}
175175
}
176176

177-
var logger = hostingServiceProvider.GetRequiredService<ILogger<WebHost>>();
178-
// Warn about duplicate HostingStartupAssemblies
179-
foreach (var assemblyName in _options.GetFinalHostingStartupAssemblies().GroupBy(a => a, StringComparer.OrdinalIgnoreCase).Where(g => g.Count() > 1))
180-
{
181-
logger.LogWarning($"The assembly {assemblyName} was specified multiple times. Hosting startup assemblies should only be specified once.");
182-
}
183-
184177
AddApplicationServices(applicationServices, hostingServiceProvider);
185178

186179
var host = new WebHost(
@@ -193,6 +186,14 @@ public IWebHost Build()
193186
{
194187
host.Initialize();
195188

189+
var logger = host.Services.GetRequiredService<ILogger<WebHost>>();
190+
191+
// Warn about duplicate HostingStartupAssemblies
192+
foreach (var assemblyName in _options.GetFinalHostingStartupAssemblies().GroupBy(a => a, StringComparer.OrdinalIgnoreCase).Where(g => g.Count() > 1))
193+
{
194+
logger.LogWarning($"The assembly {assemblyName} was specified multiple times. Hosting startup assemblies should only be specified once.");
195+
}
196+
196197
return host;
197198
}
198199
catch
@@ -208,7 +209,7 @@ IServiceProvider GetProviderFromFactory(IServiceCollection collection)
208209
var provider = collection.BuildServiceProvider();
209210
var factory = provider.GetService<IServiceProviderFactory<IServiceCollection>>();
210211

211-
if (factory != null)
212+
if (factory != null && !(factory is DefaultServiceProviderFactory))
212213
{
213214
using (provider)
214215
{

0 commit comments

Comments
 (0)