当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET CORE系列【七】分析NetCore启动原理

ASP.NET CORE系列【七】分析NetCore启动原理

2019年08月11日  | 移动技术网IT编程  | 我要评论

香港经典喜剧鬼片,白马寺正骨膏,中星9号怎么升级

前言

 有很久一段时间没更新了,因为工作和家里的问题导致没能坚持,

现在开始会继续每周更新,主要是记录自己所学和一起讨论解决过的问题,一起成长,

为.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         }

 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:码字水平有待提高!

 

 

 

 

 

 

     

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网