.NET Core 3.1.1--HostBuilder 类源码
1 // Assembly location: C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\3.1.1\Microsoft.Extensions.Hosting.dll 2 3 using Microsoft.Extensions.Configuration; 4 using Microsoft.Extensions.DependencyInjection; 5 using Microsoft.Extensions.FileProviders; 6 using Microsoft.Extensions.Hosting.Internal; 7 using System; 8 using System.Collections.Generic; 9 using System.IO; 10 using System.Reflection; 11 12 namespace Microsoft.Extensions.Hosting 13 { 14 public class HostBuilder : IHostBuilder 15 { 16 private List<Action<IConfigurationBuilder>> _configureHostConfigActions = new List<Action<IConfigurationBuilder>>(); 17 private List<Action<HostBuilderContext, IConfigurationBuilder>> _configureAppConfigActions = new List<Action<HostBuilderContext, IConfigurationBuilder>>(); 18 private List<Action<HostBuilderContext, IServiceCollection>> _configureServicesActions = new List<Action<HostBuilderContext, IServiceCollection>>(); 19 private List<IConfigureContainerAdapter> _configureContainerActions = new List<IConfigureContainerAdapter>(); 20 private IServiceFactoryAdapter _serviceProviderFactory = (IServiceFactoryAdapter) new ServiceFactoryAdapter<IServiceCollection>((IServiceProviderFactory<IServiceCollection>) new DefaultServiceProviderFactory()); 21 private bool _hostBuilt; 22 private IConfiguration _hostConfiguration; 23 private IConfiguration _appConfiguration; 24 private HostBuilderContext _hostBuilderContext; 25 private HostingEnvironment _hostingEnvironment; 26 private IServiceProvider _appServices; 27 28 public IDictionary<object, object> Properties { get; } = (IDictionary<object, object>) new Dictionary<object, object>(); 29 30 public IHostBuilder ConfigureHostConfiguration( 31 Action<IConfigurationBuilder> configureDelegate) 32 { 33 List<Action<IConfigurationBuilder>> hostConfigActions = this._configureHostConfigActions; 34 Action<IConfigurationBuilder> action = configureDelegate; 35 if (action == null) 36 throw new ArgumentNullException(nameof (configureDelegate)); 37 hostConfigActions.Add(action); 38 return (IHostBuilder) this; 39 } 40 41 public IHostBuilder ConfigureAppConfiguration( 42 Action<HostBuilderContext, IConfigurationBuilder> configureDelegate) 43 { 44 List<Action<HostBuilderContext, IConfigurationBuilder>> appConfigActions = this._configureAppConfigActions; 45 Action<HostBuilderContext, IConfigurationBuilder> action = configureDelegate; 46 if (action == null) 47 throw new ArgumentNullException(nameof (configureDelegate)); 48 appConfigActions.Add(action); 49 return (IHostBuilder) this; 50 } 51 52 public IHostBuilder ConfigureServices( 53 Action<HostBuilderContext, IServiceCollection> configureDelegate) 54 { 55 List<Action<HostBuilderContext, IServiceCollection>> configureServicesActions = this._configureServicesActions; 56 Action<HostBuilderContext, IServiceCollection> action = configureDelegate; 57 if (action == null) 58 throw new ArgumentNullException(nameof (configureDelegate)); 59 configureServicesActions.Add(action); 60 return (IHostBuilder) this; 61 } 62 63 public IHostBuilder UseServiceProviderFactory<TContainerBuilder>( 64 IServiceProviderFactory<TContainerBuilder> factory) 65 { 66 IServiceProviderFactory<TContainerBuilder> serviceProviderFactory = factory; 67 if (serviceProviderFactory == null) 68 throw new ArgumentNullException(nameof (factory)); 69 this._serviceProviderFactory = (IServiceFactoryAdapter) new ServiceFactoryAdapter<TContainerBuilder>(serviceProviderFactory); 70 return (IHostBuilder) this; 71 } 72 73 public IHostBuilder UseServiceProviderFactory<TContainerBuilder>( 74 Func<HostBuilderContext, IServiceProviderFactory<TContainerBuilder>> factory) 75 { 76 Func<HostBuilderContext> contextResolver = (Func<HostBuilderContext>) (() => this._hostBuilderContext); 77 Func<HostBuilderContext, IServiceProviderFactory<TContainerBuilder>> factoryResolver = factory; 78 if (factoryResolver == null) 79 throw new ArgumentNullException(nameof (factory)); 80 this._serviceProviderFactory = (IServiceFactoryAdapter) new ServiceFactoryAdapter<TContainerBuilder>(contextResolver, factoryResolver); 81 return (IHostBuilder) this; 82 } 83 84 public IHostBuilder ConfigureContainer<TContainerBuilder>( 85 Action<HostBuilderContext, TContainerBuilder> configureDelegate) 86 { 87 List<IConfigureContainerAdapter> containerActions = this._configureContainerActions; 88 Action<HostBuilderContext, TContainerBuilder> action = configureDelegate; 89 if (action == null) 90 throw new ArgumentNullException(nameof (configureDelegate)); 91 ConfigureContainerAdapter<TContainerBuilder> containerAdapter = new ConfigureContainerAdapter<TContainerBuilder>(action); 92 containerActions.Add((IConfigureContainerAdapter) containerAdapter); 93 return (IHostBuilder) this; 94 } 95 96 public IHost Build() 97 { 98 if (this._hostBuilt) 99 throw new InvalidOperationException("Build can only be called once."); 100 this._hostBuilt = true; 101 this.BuildHostConfiguration(); 102 this.CreateHostingEnvironment(); 103 this.CreateHostBuilderContext(); 104 this.BuildAppConfiguration(); 105 this.CreateServiceProvider(); 106 return this._appServices.GetRequiredService<IHost>(); 107 } 108 109 private void BuildHostConfiguration() 110 { 111 IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().AddInMemoryCollection(); 112 foreach (Action<IConfigurationBuilder> hostConfigAction in this._configureHostConfigActions) 113 hostConfigAction(configurationBuilder); 114 this._hostConfiguration = (IConfiguration) configurationBuilder.Build(); 115 } 116 117 private void CreateHostingEnvironment() 118 { 119 this._hostingEnvironment = new HostingEnvironment() 120 { 121 ApplicationName = this._hostConfiguration[HostDefaults.ApplicationKey], 122 EnvironmentName = this._hostConfiguration[HostDefaults.EnvironmentKey] ?? Environments.Production, 123 ContentRootPath = this.ResolveContentRootPath(this._hostConfiguration[HostDefaults.ContentRootKey], AppContext.BaseDirectory) 124 }; 125 if (string.IsNullOrEmpty(this._hostingEnvironment.ApplicationName)) 126 { 127 HostingEnvironment hostingEnvironment = this._hostingEnvironment; 128 Assembly entryAssembly = Assembly.GetEntryAssembly(); 129 string str = (object) entryAssembly != null ? entryAssembly.GetName().Name : (string) null; 130 hostingEnvironment.ApplicationName = str; 131 } 132 this._hostingEnvironment.ContentRootFileProvider = (IFileProvider) new PhysicalFileProvider(this._hostingEnvironment.ContentRootPath); 133 } 134 135 private string ResolveContentRootPath(string contentRootPath, string basePath) 136 { 137 if (string.IsNullOrEmpty(contentRootPath)) 138 return basePath; 139 return Path.IsPathRooted(contentRootPath) ? contentRootPath : Path.Combine(Path.GetFullPath(basePath), contentRootPath); 140 } 141 142 private void CreateHostBuilderContext() 143 { 144 this._hostBuilderContext = new HostBuilderContext(this.Properties) 145 { 146 HostingEnvironment = (IHostEnvironment) this._hostingEnvironment, 147 Configuration = this._hostConfiguration 148 }; 149 } 150 151 private void BuildAppConfiguration() 152 { 153 IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(this._hostingEnvironment.ContentRootPath).AddConfiguration(this._hostConfiguration, true); 154 foreach (Action<HostBuilderContext, IConfigurationBuilder> configureAppConfigAction in this._configureAppConfigActions) 155 configureAppConfigAction(this._hostBuilderContext, configurationBuilder); 156 this._appConfiguration = (IConfiguration) configurationBuilder.Build(); 157 this._hostBuilderContext.Configuration = this._appConfiguration; 158 } 159 160 private void CreateServiceProvider() 161 { 162 ServiceCollection services = new ServiceCollection(); 163 services.AddSingleton<IHostingEnvironment>((IHostingEnvironment) this._hostingEnvironment); 164 services.AddSingleton<IHostEnvironment>((IHostEnvironment) this._hostingEnvironment); 165 services.AddSingleton<HostBuilderContext>(this._hostBuilderContext); 166 services.AddSingleton<IConfiguration>((Func<IServiceProvider, IConfiguration>) (_ => this._appConfiguration)); 167 services.AddSingleton<IApplicationLifetime>((Func<IServiceProvider, IApplicationLifetime>) (s => (IApplicationLifetime) s.GetService<IHostApplicationLifetime>())); 168 services.AddSingleton<IHostApplicationLifetime, ApplicationLifetime>(); 169 services.AddSingleton<IHostLifetime, ConsoleLifetime>(); 170 services.AddSingleton<IHost, Microsoft.Extensions.Hosting.Internal.Host>(); 171 services.AddOptions(); 172 services.AddLogging(); 173 foreach (Action<HostBuilderContext, IServiceCollection> configureServicesAction in this._configureServicesActions) 174 configureServicesAction(this._hostBuilderContext, (IServiceCollection) services); 175 object builder = this._serviceProviderFactory.CreateBuilder((IServiceCollection) services); 176 foreach (IConfigureContainerAdapter configureContainerAction in this._configureContainerActions) 177 configureContainerAction.ConfigureContainer(this._hostBuilderContext, builder); 178 this._appServices = this._serviceProviderFactory.CreateServiceProvider(builder); 179 if (this._appServices == null) 180 throw new InvalidOperationException("The IServiceProviderFactory returned a null IServiceProvider."); 181 this._appServices.GetService<IConfiguration>(); 182 } 183 } 184 }