ASP.NET CORE系列【七】分析NetCore啟動原理

  • 2019 年 10 月 3 日
  • 筆記

前言

 有很久一段時間沒更新了,因為工作和家裡的問題導致沒能堅持,

現在開始會繼續每周更新,主要是記錄自己所學和一起討論解決過的問題,一起成長,

為.net圈子添磚加瓦!

介紹

到目前為止應該很多同學已經把項目升級到core了,對於項目結構都已經很熟悉了,今天我們主要講解Startup.Cs   Program.Cs兩個文件

分析Core項目的啟動原理

 

Program.Cs

 

 

   很熟悉Main入口,主要是三個方法CreateWebHostBuilder()    Build()  Run()  很簡單的三個方法,但是他卻能夠順利的把整個項目給啟動起來

   它肯定是在內部封裝了很多東西,那它是如何封裝的呢  是如何運行的

    一.首先我們得學會查看源碼

     1.https://github.com/aspnet/AspNetCore

     2.Vs2019

     3.Reshaper

      這裡有三個方法查看netcore源代碼,第一個就是微軟官方開源的項目地址

      今天我們主要講使用Vs2019方法,Reshaper不建議去使用,電腦配置不高的情況下會很卡,而且vs2019的新功能也基本上夠用了

     使用vs2019先設置   工具->選項->文本編輯器->c#->高級->支持導航到反編譯(實驗)

    勾上之後,按f12就能定位到源碼

 

二.啟動順序

 1 Main()入口   

 

 2 WebHostBuilder()準備 

     CreateDefaultBuilder方法 從命名就能看出,它注入了很多服務,大家可以定位進入仔細看看

     創建WebHost默認配置,加載自定義配置UseStartup()主要在Startup.cs裏面自行配置

    主要是配置Service Di和http管道,這些都市在WebHost啟動之前做的事

    我們f12定位打源代碼查看

 1 public static IWebHostBuilder CreateDefaultBuilder(string[] args)   2         {   3             WebHostBuilder webHostBuilder = new WebHostBuilder();   4             if (string.IsNullOrEmpty(webHostBuilder.GetSetting(WebHostDefaults.ContentRootKey)))   5             {   6                 webHostBuilder.UseContentRoot(Directory.GetCurrentDirectory());   7             }   8             if (args != null)   9             {  10                 webHostBuilder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());  11             }  12             webHostBuilder.UseKestrel(delegate (WebHostBuilderContext builderContext, KestrelServerOptions options)  13             {  14                 options.Configure(builderContext.Configuration.GetSection("Kestrel"));  15             }).ConfigureAppConfiguration(delegate (WebHostBuilderContext hostingContext, IConfigurationBuilder config)  16             {  17                 IHostingEnvironment hostingEnvironment = hostingContext.HostingEnvironment;  18                 config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddJsonFile("appsettings." + hostingEnvironment.EnvironmentName + ".json", optional: true, reloadOnChange: true);  19                 if (hostingEnvironment.IsDevelopment())  20                 {  21                     Assembly assembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName));  22                     if (assembly != null)  23                     {  24                         config.AddUserSecrets(assembly, optional: true);  25                     }  26                 }  27                 config.AddEnvironmentVariables();  28                 if (args != null)  29                 {  30                     config.AddCommandLine(args);  31                 }  32             }).ConfigureLogging(delegate (WebHostBuilderContext hostingContext, ILoggingBuilder logging)  33             {  34                 logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));  35                 logging.AddConsole();  36                 logging.AddDebug();  37                 logging.AddEventSourceLogger();  38             })  39                 .ConfigureServices(delegate (WebHostBuilderContext hostingContext, IServiceCollection services)  40                 {  41                     services.PostConfigure(delegate (HostFilteringOptions options)  42                     {  43                         if (options.AllowedHosts == null || options.AllowedHosts.Count == 0)  44                         {  45                             string[] array = hostingContext.Configuration["AllowedHosts"]?.Split(new char[1]  46                             {  47                                 ';'  48                             }, StringSplitOptions.RemoveEmptyEntries);  49                             options.AllowedHosts = ((array != null && array.Length != 0) ? array : new string[1]  50                             {  51                                 "*"  52                             });  53                         }  54                     });  55                     services.AddSingleton((IOptionsChangeTokenSource<HostFilteringOptions>)new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));  56                     services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();  57                 })  58                 .UseIIS()  59                 .UseIISIntegration()  60                 .UseDefaultServiceProvider(delegate (WebHostBuilderContext context, ServiceProviderOptions options)  61                 {  62                     options.ValidateScopes = context.HostingEnvironment.IsDevelopment();  63                 });  64             return webHostBuilder;  65         }

View Code

 3 Build()構建   

   構建AspNetCre.Hosting 託管web應用程序

 4 Run()啟動

   運行web應用程序並阻止調用線程,直到主機關閉

   

 

CreateDefaultBuilder方法

 說一下默認配置 配置了哪些東西,結合代碼看一下

 第一行   WebHostBuilder webHostBuilder = new WebHostBuilder(); 創建了webHostBuilder實例,我們F12進去看看

 類裏面的默認構造函數,看到這裡,構造函數實例對象(依賴注入)

  

 

 

 

 

 我們這裡可以看到 提供了ApplicationBuilder工廠  然後把我們各種配置在這裡進行直接注入了

 在Starpup.cs裏面 app.UseMvc() 這種  都是IApplicationBuilder

 現在恍然大悟了

具體工廠里有什麼呢,接口怎麼定義呢,大家有興趣可以自行去了解

 

第二行  webHostBuilder.UseContentRoot(Directory.GetCurrentDirectory());

 Use是一個中間件,ContentRoot是我們內容的根目錄,指定了了我們web主機要使用的內容站點根目錄

這個設置決定了安排asp.net core開始搜索內容文件,比如view

也就是說 項目啟動,肯定有一個中間件去調用wwwroot這個文件夾

 

後面還有UseConfiguration中間件  使用一些命令 比如dootnet run 在這裡執行     

 UseKestrel  這是開啟Kestrel中間件  給web主機使用的服務器,後面代碼又開啟了UseIIs的中間件

可能會疑惑為什麼會開啟不同的呢,

這裡主要是,netcore有兩種運營模式 一個進程內 一個是進程外,這個後續的文章會降到

看大這裡,大家應該都知道.net core啟動是個怎麼回事了,剩下的 可以自行看源碼了解哦

 

ps:碼字水平有待提高!