Configuration初始化的过程
入口启动
1
2
3
4public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();Configuration
的初始化位于IWebHostBuilder.Build()
方法中,其中有一个方法:BuildCommonServices
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48public IWebHost Build()
{
//...............................省略................................//
//*******************************************************************//
var hostingServices = BuildCommonServices(out var hostingStartupErrors);
//*******************************************************************//
var applicationServices = hostingServices.Clone();
var hostingServiceProvider = hostingServices.BuildServiceProvider();
AddApplicationServices(applicationServices, hostingServiceProvider);
var host = new WebHost(
applicationServices,
hostingServiceProvider,
_options,
_config,
hostingStartupErrors);
host.Initialize();
return host;
}
private IServiceCollection BuildCommonServices(out AggregateException hostingStartupErrors)
{
//...............................省略................................//
//*******************************************************************//
var builder = new ConfigurationBuilder()
.SetBasePath(_hostingEnvironment.ContentRootPath)
.AddInMemoryCollection(_config.AsEnumerable());
foreach (var configureAppConfiguration in _configureAppConfigurationBuilderDelegates)
{
configureAppConfiguration(_context, builder);
}
var configuration = builder.Build();
//*******************************************************************//
//至此Configure初始化完毕
//...............................省略................................//
foreach (var configureServices in _configureServicesDelegates)
{
configureServices(_context, services);
}
return services;
}ConfigurationBuilder.Build() UML 分析