标签:变量 tar dde ica start nbsp ons image conf
说明:
1 namespace HelloWorld 2 { 3 public class Program 4 { 5 public static void Main(string[] args) 6 { 7 CreateWebHostBuilder(args).Build().Run(); 8 } 9 10 public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 11 WebHost.CreateDefaultBuilder(args) 12 .UseStartup<Startup>(); 13 } 14 }
1 public static IWebHostBuilder CreateDefaultBuilder(string[] args) 2 { 3 var builder = new WebHostBuilder() 4 // 使用 Kestrel 作为应用程序的托管 web 服务器 5 .UseKestrel((builderContext, options) => 6 { 7 options.Configure(builderContext.Configuration.GetSection("Kestrel")); 8 }) 9 // 将内容根目录设置为应用程序的当前工作目录 10 .UseContentRoot(Directory.GetCurrentDirectory()) 11 // 加载配置项 12 .ConfigureAppConfiguration((hostingContext, config) => 13 { 14 var env = hostingContext.HostingEnvironment; 15 // 1. 加载appsettings.json 16 config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 17 // 2. 加载appsettings.{EnvironmentName}.json 18 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); 19 20 if (env.IsDevelopment()) 21 { 22 var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName)); 23 if (appAssembly != null) 24 { 25 // 3. 加载应用在使用入口程序集的 Development 环境中运行时的机密管理器 26 config.AddUserSecrets(appAssembly, optional: true); 27 } 28 } 29 30 // 4. 加载环境变量 31 config.AddEnvironmentVariables(); 32 33 if (args != null) 34 { 35 // 5. 加载命令行参数 36 config.AddCommandLine(args); 37 } 38 }) 39 // 配置日志 40 .ConfigureLogging((hostingContext, logging) => 41 { 42 // 从配置读取日志相关配置 43 logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); 44 // 配置控制台和调试输出的日志记录(源码中使用的是 TryAddEnumerable,说明如果配置中已配置则不重复添加) 45 logging.AddConsole(); 46 logging.AddDebug(); 47 }) 48 .ConfigureServices((hostingContext, services) => 49 { 50 // Fallback 51 services.PostConfigure<HostFilteringOptions>(options => 52 { 53 if (options.AllowedHosts == null || options.AllowedHosts.Count == 0) 54 { 55 // "AllowedHosts": "localhost;127.0.0.1;[::1]" 56 var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ‘;‘ }, StringSplitOptions.RemoveEmptyEntries); 57 // Fall back to "*" to disable. 58 options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" }); 59 } 60 }); 61 // Change notification 62 services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>( 63 new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration)); 64 65 services.AddTransient<IStartupFilter, HostFilteringStartupFilter>(); 66 }) 67 // IIS集成 68 .UseIISIntegration() 69 // 作用域验证 70 .UseDefaultServiceProvider((context, options) => 71 { 72 options.ValidateScopes = context.HostingEnvironment.IsDevelopment(); 73 }); 74 75 if (args != null) 76 { 77 // 加载命令行参数, 比ConfigureAppConfiguration中AddCommandLine先执行 78 builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build()); 79 } 80 81 return builder; 82 }
配置加载顺序
Development
环境中运行时的机密管理器。标签:变量 tar dde ica start nbsp ons image conf
原文地址:https://www.cnblogs.com/Dumby/p/10809960.html