asp.net core 入口程序

1.项目结构

说明:

  1. .vscode 文件夹存放vscode配置文件,用于配置当前项目的启动调试配置项。
  2. bin 用于存放编译结果
  3. Controllers 用于存放控制器
  4. Models 用于存放视图模型
  5. obj 用于存放中间编译文件
  6. Properties 用于存放主机配置,如端口号,启动后在浏览器中打开
  7. Views 用于存放视图
  8. wwwroot 用于存放静态文件,如 HTML 文件、JavaScript 文件和 CSS 文件
  9. appsettings[.Environment].json 包含配置数据,如连接字符串
  10. Program.cs 程序的入口点
  11. Startup.cs 用于配置服务和应用的请求管道

2.流程分析

 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 }
View Code

配置加载顺序

  • appsettings.json。
  • appsettings.{Environment}.json。
  • 应用在使用入口程序集的 Development 环境中运行时的机密管理器。
  • 环境变量。
  • 命令行参数。

 引用

WebHost.cs

WebHostBuilder.cs

Web主机

作用域验证

猜你喜欢

转载自www.cnblogs.com/Dumby/p/10809960.html