Run IHostingStartup's before materializing IConfiguration (#1052)
This commit is contained in:
parent
285d62b312
commit
ba2bca4a96
|
|
@ -273,35 +273,6 @@ namespace Microsoft.AspNetCore.Hosting
|
|||
|
||||
_options = new WebHostOptions(_config);
|
||||
|
||||
var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, AppContext.BaseDirectory);
|
||||
var applicationName = _options.ApplicationName;
|
||||
|
||||
// Initialize the hosting environment
|
||||
_hostingEnvironment.Initialize(applicationName, contentRootPath, _options);
|
||||
_context.HostingEnvironment = _hostingEnvironment;
|
||||
|
||||
var services = new ServiceCollection();
|
||||
services.AddSingleton(_hostingEnvironment);
|
||||
services.AddSingleton(_context);
|
||||
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(_hostingEnvironment.ContentRootPath)
|
||||
.AddInMemoryCollection(_config.AsEnumerable());
|
||||
|
||||
foreach (var configureAppConfiguration in _configureAppConfigurationBuilderDelegates)
|
||||
{
|
||||
configureAppConfiguration(_context, builder);
|
||||
}
|
||||
|
||||
var configuration = builder.Build();
|
||||
services.AddSingleton<IConfiguration>(configuration);
|
||||
_context.Configuration = configuration;
|
||||
|
||||
// The configured ILoggerFactory is added as a singleton here. AddLogging below will not add an additional one.
|
||||
var loggerFactory = _createLoggerFactoryDelegate?.Invoke(_context) ?? new LoggerFactory(configuration.GetSection("Logging"));
|
||||
services.AddSingleton(loggerFactory);
|
||||
_context.LoggerFactory = loggerFactory;
|
||||
|
||||
if (!_options.PreventHostingStartup)
|
||||
{
|
||||
var exceptions = new List<Exception>();
|
||||
|
|
@ -338,6 +309,35 @@ namespace Microsoft.AspNetCore.Hosting
|
|||
}
|
||||
}
|
||||
|
||||
var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, AppContext.BaseDirectory);
|
||||
var applicationName = _options.ApplicationName;
|
||||
|
||||
// Initialize the hosting environment
|
||||
_hostingEnvironment.Initialize(applicationName, contentRootPath, _options);
|
||||
_context.HostingEnvironment = _hostingEnvironment;
|
||||
|
||||
var services = new ServiceCollection();
|
||||
services.AddSingleton(_hostingEnvironment);
|
||||
services.AddSingleton(_context);
|
||||
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(_hostingEnvironment.ContentRootPath)
|
||||
.AddInMemoryCollection(_config.AsEnumerable());
|
||||
|
||||
foreach (var configureAppConfiguration in _configureAppConfigurationBuilderDelegates)
|
||||
{
|
||||
configureAppConfiguration(_context, builder);
|
||||
}
|
||||
|
||||
var configuration = builder.Build();
|
||||
services.AddSingleton<IConfiguration>(configuration);
|
||||
_context.Configuration = configuration;
|
||||
|
||||
// The configured ILoggerFactory is added as a singleton here. AddLogging below will not add an additional one.
|
||||
var loggerFactory = _createLoggerFactoryDelegate?.Invoke(_context) ?? new LoggerFactory(configuration.GetSection("Logging"));
|
||||
services.AddSingleton(loggerFactory);
|
||||
_context.LoggerFactory = loggerFactory;
|
||||
|
||||
foreach (var configureLogging in _configureLoggingDelegates)
|
||||
{
|
||||
configureLogging(_context, loggerFactory);
|
||||
|
|
|
|||
|
|
@ -931,6 +931,21 @@ namespace Microsoft.AspNetCore.Hosting
|
|||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Build_ConfigureAppConfigurationInHostingStartupWorks()
|
||||
{
|
||||
var builder = CreateWebHostBuilder()
|
||||
.CaptureStartupErrors(false)
|
||||
.Configure(app => { })
|
||||
.UseServer(new TestServer());
|
||||
|
||||
using (var host = (WebHost)builder.Build())
|
||||
{
|
||||
var configuration = host.Services.GetRequiredService<IConfiguration>();
|
||||
Assert.Equal("value", configuration["testhostingstartup:config"]);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Build_DoesRunHostingStartupFromPrimaryAssemblyEvenIfNotSpecified()
|
||||
{
|
||||
|
|
@ -1089,7 +1104,12 @@ namespace Microsoft.AspNetCore.Hosting
|
|||
.UseSetting("testhostingstartup_chain", builder.GetSetting("testhostingstartup_chain") + "0")
|
||||
.ConfigureServices(services => services.AddSingleton<ServiceA>())
|
||||
.ConfigureServices(services => services.AddSingleton<ITestSink>(loggerProvider.Sink))
|
||||
.ConfigureLogging(lf => lf.AddProvider(loggerProvider));
|
||||
.ConfigureLogging(lf => lf.AddProvider(loggerProvider))
|
||||
.ConfigureAppConfiguration((context, configurationBuilder) => configurationBuilder.AddInMemoryCollection(
|
||||
new []
|
||||
{
|
||||
new KeyValuePair<string,string>("testhostingstartup:config", "value")
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue