<ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>


      前言

      ?有很久一段時(shí)間沒更新了,因?yàn)楣ぷ骱图依锏膯栴}導(dǎo)致沒能堅(jiān)持,

      現(xiàn)在開始會(huì)繼續(xù)每周更新,主要是記錄自己所學(xué)和一起討論解決過的問題,一起成長(zhǎng),

      為.net圈子添磚加瓦!

      介紹

      到目前為止應(yīng)該很多同學(xué)已經(jīng)把項(xiàng)目升級(jí)到core了,對(duì)于項(xiàng)目結(jié)構(gòu)都已經(jīng)很熟悉了,今天我們主要講解Startup.Cs? ?Program.Cs兩個(gè)文件

      分析Core項(xiàng)目的啟動(dòng)原理

      ?

      Program.Cs



      ?

      ?

      ? ?很熟悉Main入口,主要是三個(gè)方法CreateWebHostBuilder()? ??Build()??Run()?
      很簡(jiǎn)單的三個(gè)方法,但是他卻能夠順利的把整個(gè)項(xiàng)目給啟動(dòng)起來

      ? ?它肯定是在內(nèi)部封裝了很多東西,那它是如何封裝的呢? 是如何運(yùn)行的

      ? ? 一.首先我們得學(xué)會(huì)查看源碼

      ? ? ?1.https://github.com/aspnet/AspNetCore
      <https://github.com/aspnet/AspNetCore>

      ? ? ?2.Vs2019

      ? ? ?3.Reshaper

      ? ? ? 這里有三個(gè)方法查看netcore源代碼,第一個(gè)就是微軟官方開源的項(xiàng)目地址

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

      ? ? ?使用vs2019先設(shè)置? ?工具->選項(xiàng)->文本編輯器->c#->高級(jí)->支持導(dǎo)航到反編譯(實(shí)驗(yàn))

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



      ?

      二.啟動(dòng)順序

      ?1 Main()入口? ?

      ?

      ?2 WebHostBuilder()準(zhǔn)備?

      ? ? ?CreateDefaultBuilder方法?從命名就能看出,它注入了很多服務(wù),大家可以定位進(jìn)入仔細(xì)看看

      ? ? ?創(chuàng)建WebHost默認(rèn)配置,加載自定義配置UseStartup()主要在Startup.cs里面自行配置

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

      ? ? 我們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()構(gòu)建? ?

      ? ?構(gòu)建AspNetCre.Hosting 托管web應(yīng)用程序

      ?4 Run()啟動(dòng)

      ? ?運(yùn)行web應(yīng)用程序并阻止調(diào)用線程,直到主機(jī)關(guān)閉

      ? ?

      ?

      CreateDefaultBuilder方法

      ?說一下默認(rèn)配置 配置了哪些東西,結(jié)合代碼看一下

      ?第一行???WebHostBuilder webHostBuilder = new WebHostBuilder();
      創(chuàng)建了webHostBuilder實(shí)例,我們F12進(jìn)去看看

      ?類里面的默認(rèn)構(gòu)造函數(shù),看到這里,構(gòu)造函數(shù)實(shí)例對(duì)象(依賴注入)

      ??

      ?

      ?


      ?

      ?

      ?我們這里可以看到 提供了ApplicationBuilder工廠? 然后把我們各種配置在這里進(jìn)行直接注入了

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

      ?現(xiàn)在恍然大悟了

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

      ?

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

      ?Use是一個(gè)中間件,ContentRoot是我們內(nèi)容的根目錄,指定了了我們web主機(jī)要使用的內(nèi)容站點(diǎn)根目錄

      這個(gè)設(shè)置決定了安排asp.net core開始搜索內(nèi)容文件,比如view

      也就是說 項(xiàng)目啟動(dòng),肯定有一個(gè)中間件去調(diào)用wwwroot這個(gè)文件夾

      ?

      后面還有UseConfiguration中間件? 使用一些命令 比如dootnet run 在這里執(zhí)行? ? ?

      ?UseKestrel? 這是開啟Kestrel中間件? 給web主機(jī)使用的服務(wù)器,后面代碼又開啟了UseIIs的中間件

      可能會(huì)疑惑為什么會(huì)開啟不同的呢,

      這里主要是,netcore有兩種運(yùn)營(yíng)模式 一個(gè)進(jìn)程內(nèi) 一個(gè)是進(jìn)程外,這個(gè)后續(xù)的文章會(huì)降到

      看大這里,大家應(yīng)該都知道.net core啟動(dòng)是個(gè)怎么回事了,剩下的 可以自行看源碼了解哦

      ?

      ps:碼字水平有待提高!

      ?

      ?

      ?

      ?

      ?

      ?

      ? ? ?

      ?

      友情鏈接
      ioDraw流程圖
      API參考文檔
      OK工具箱
      云服務(wù)器優(yōu)惠
      阿里云優(yōu)惠券
      騰訊云優(yōu)惠券
      京東云優(yōu)惠券
      站點(diǎn)信息
      問題反饋
      郵箱:[email protected]
      QQ群:637538335
      關(guān)注微信

        <ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>
          国产香蕉网 | 高清无码啪啪视频 | 亚洲av三区 | 黑丝美女操逼 | 小兰的堕落h嗯啊好深啊 | 欧美成人精品三级在线观看播放 | 大鸡巴操老逼 | 国产传媒午夜成人 | 动漫巨大房乳挤奶动漫 | 国产私拍视频 |