Visual
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
WebHostBuilder 知道该如何配置 Web Server 的环境,CreateDefaultBuilder 即进行默认配置。
默认配置
反编译的 CreateDefaultBuilder 方法源码:
/// <summary>/// Initializes a new instance of the <see cref="T:Microsoft.AspNetCore.Hosting.WebHostBuilder" /> class with pre-configured defaults./// </summary>public static IWebHostBuilder CreateDefaultBuilder(string[] args){WebHostBuilder hostBuilder = new WebHostBuilder();if (string.IsNullOrEmpty(hostBuilder.GetSetting(WebHostDefaults.ContentRootKey)))hostBuilder.UseContentRoot(Directory.GetCurrentDirectory());if (args != null)hostBuilder.UseConfiguration((IConfiguration) new ConfigurationBuilder().AddCommandLine(args).Build());hostBuilder.UseKestrel((Action<WebHostBuilderContext, KestrelServerOptions>) ((builderContext, options) => options.Configure((IConfiguration) builderContext.Configuration.GetSection("Kestrel")))).ConfigureAppConfiguration((Action<WebHostBuilderContext, IConfigurationBuilder>) ((hostingContext, config) =>{IHostingEnvironment hostingEnvironment = hostingContext.HostingEnvironment;config.AddJsonFile("appsettings.json", true, true).AddJsonFile("appsettings." + hostingEnvironment.EnvironmentName + ".json", true, true);if (hostingEnvironment.IsDevelopment()){Assembly assembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName));if (assembly != (Assembly) null)config.AddUserSecrets(assembly, true);}config.AddEnvironmentVariables();if (args == null)return;config.AddCommandLine(args);})).ConfigureLogging((Action<WebHostBuilderContext, ILoggingBuilder>) ((hostingContext, logging) =>{logging.AddConfiguration((IConfiguration) hostingContext.Configuration.GetSection("Logging"));logging.AddConsole();logging.AddDebug();logging.AddEventSourceLogger();})).ConfigureServices((Action<WebHostBuilderContext, IServiceCollection>) ((hostingContext, services) =>{services.PostConfigure<HostFilteringOptions>((Action<HostFilteringOptions>) (options =>{if (options.AllowedHosts != null && options.AllowedHosts.Count != 0)return;string str = hostingContext.Configuration["AllowedHosts"];string[] strArray1;if (str == null){strArray1 = (string[]) null;}else{char[] separator = new char[1]{ ';' };int num = 1;strArray1 = str.Split(separator, (StringSplitOptions) num);}string[] strArray2 = strArray1;HostFilteringOptions filteringOptions = options;string[] strArray3;if (strArray2 == null || strArray2.Length == 0)strArray3 = new string[1]{ "*" };elsestrArray3 = strArray2;filteringOptions.AllowedHosts = (IList<string>) strArray3;}));services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>((IOptionsChangeTokenSource<HostFilteringOptions>) new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();})).UseIIS().UseIISIntegration().UseDefaultServiceProvider((Action<WebHostBuilderContext, ServiceProviderOptions>) ((context, options) => options.ValidateScopes = context.HostingEnvironment.IsDevelopment()));return (IWebHostBuilder) hostBuilder;}
- 使用 Kestrel Web Server
- ASP.NET Core 内置,跨平台
- IIS 集成
- UseIIS(), UseIISIntegration()
- Log
- IConfiguration 接口
IConfiguration 配置信息的来源
上面源码第一句 WebHostBuilder hostBuilder = new WebHostBuilder(); 创建 WebHostBuilder 对象时,就初始化了 Configuration(先配置 _config 字段,然后将值传入新建的 WebHostBuilderContext):
/// <summary>/// Initializes a new instance of the <see cref="T:Microsoft.AspNetCore.Hosting.WebHostBuilder" /> class./// </summary>public WebHostBuilder(){this._hostingEnvironment = new HostingEnvironment();this._configureServicesDelegates = new List<Action<WebHostBuilderContext, IServiceCollection>>();this._configureAppConfigurationBuilderDelegates = new List<Action<WebHostBuilderContext, IConfigurationBuilder>>();this._config = (IConfiguration) new ConfigurationBuilder().AddEnvironmentVariables("ASPNETCORE_").Build();if (string.IsNullOrEmpty(this.GetSetting(WebHostDefaults.EnvironmentKey)))this.UseSetting(WebHostDefaults.EnvironmentKey, Environment.GetEnvironmentVariable("Hosting:Environment") ?? Environment.GetEnvironmentVariable("ASPNET_ENV"));if (string.IsNullOrEmpty(this.GetSetting(WebHostDefaults.ServerUrlsKey)))this.UseSetting(WebHostDefaults.ServerUrlsKey, Environment.GetEnvironmentVariable("ASPNETCORE_SERVER.URLS"));this._context = new WebHostBuilderContext(){Configuration = this._config};}
配置信息来源:
- appsettings.json
- User Secrets
- 系统环境变量
- 命令行参数
config.AddJsonFile("appsettings.json", true, true).AddJsonFile("appsettings." + hostingEnvironment.EnvironmentName + ".json", true, true);if (hostingEnvironment.IsDevelopment()){Assembly assembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName));if (assembly != (Assembly) null)config.AddUserSecrets(assembly, true);}config.AddEnvironmentVariables();if (args == null)return;config.AddCommandLine(args);
如果出现重复的属性,后添加的会覆盖前面的值,所以系统环境变量的优先级高于 appsettings.json。

