Photo clarify ASP.NET Core boot process

Quote :

1 Introduction

For ASP.NET Coreapplications, we have to remember a very important point is: it is essentially a stand-alone console application, it is not necessary to hosted inside IIS and IIS is not required to start the run (which is ASP.NET Corecross-platform Cornerstone). ASP.NET CoreHas a built-in application Self-Hosted(self-hosted) in Web Server(Web server) for handling external requests.

Whether it is hosted or self-hosted, they are inseparable from the Host(host). In ASP.NET Coreapplications configure and start a Hostmanagement application to complete the startup, and their life cycle (as shown below). The Hostmain duty is Web Serverdisposed and Pilpeline(request processing pipeline) construct.
ASP.NET Core overall boot process

This figure depicts an overall boot process, we know from the image above ASP.NET Coreto launch the application consists of three main steps:

  1. CreateDefaultBuilder():createIWebHostBuilder
  2. Build(): IWebHostBuilderResponsible for creatingIWebHost
  3. Run():start upIWebHost

So, ASP.NET Corethe start of the essence of the application is to start as a host of WebHostobjects.
Which is mainly related to two key objects IWebHostBuilderand IWebHosttheir internal implementation is ASP.NET Corethe core of the application. Here we combine combing the call stack and source code to find out!

2. Host Constructor:IWebHostBuilder

In the start IWebHostbefore the host, we need to complete IWebHost creation and configuration. And this work need to use IWebHostBuilderobjects to complete, ASP.NET Coreprovides a default implementation WebHostBuilder. And WebHostBuilderby WebHostthe same name tools ( Microsoft.AspNetCorein the namespace) of the CreateDefaultBuildermethod of creation.
Here Insert Picture Description
CreateDefaultBuilder()Call stack

从上图中我们可以看出CreateDefaultBuilder()方法主要干了六件大事:

  1. UseKestrel:使用Kestrel作为Web server
  2. UseContentRoot:指定Web host使用的content
    root(内容根目录),比如Views。默认为当前应用程序根目录。
  3. ConfigureAppConfiguration:设置当前应用程序配置。主要是读取 appsettinggs.json配置文件、开发环境中配置的UserSecrets、添加环境变量和命令行参数 。
  4. ConfigureLogging:读取配置文件中的Logging节点,配置日志系统。
  5. UseIISIntegration:使用IISIntegration 中间件。
  6. UseDefaultServiceProvider:设置默认的依赖注入容器。

创建完毕WebHostBuilder后,通过调用UseStartup()来指定启动类,来为后续服务的注册及中间件的注册提供入口。

3. 宿主:IWebHost

在ASP.Net Core中定义了IWebHost用来表示Web应用的宿主,并提供了一个默认实现WebHost。宿主的创建是通过调用IWebHostBuilderBuild()方法来完成的。那该方法主要做了哪些事情呢,我们来看下面这张【ASP.NET Core启动流程调用堆栈】中的黄色边框部分:
Here Insert Picture Description
ASP.NET Core启动流程调用堆栈

其核心主要在于WebHost的创建,又可以划分为三个部分:

  1. 构建依赖注入容器,初始通用服务的注册:BuildCommonService();
  2. 实例化WebHostvar host = new WebHost(...);
  3. 初始化WebHost,也就是构建由中间件组成的请求处理管道:host.Initialize();

3.1. 注册初始通用服务

BuildBuildCommonService方法主要做了两件事:

  1. 查找HostingStartupAttribute特性以应用其他程序集中的启动配置
  2. 注册通用服务
  3. 若配置了启动程序集,则发现并以IStartup类型注入到IOC容器中

3.2. 创建IWebHost

public IWebHost Build() { //省略部分代码 var host = new WebHost( applicationServices, hostingServiceProvider, _options, _config, hostingStartupErrors); } host.Initialize(); return host; } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3.3. 构建请求处理管道

请求管道的构建,主要是中间件之间的衔接处理。

而请求处理管道的构建,又包含三个主要部分:

  1. 注册Startup中绑定的服务;
  2. 配置IServer
  3. 构建管道

请求管道的构建主要是借助于IApplicationBuilder,相关类图如下:
Here Insert Picture Description
请求管道的构建

4. 启动WebHost

WebHost的启动主要分为两步:

  1. 再次确认请求管道正确创建
  2. 启动Server以监听请求
  3. 启动 HostedService
    Here Insert Picture Description
    启动WebHost调用堆栈

4.1. 确认请求管道的创建

从图中可以看出,第一步调用Initialize()方法主要是取保请求管道的正确创建。其内部主要是对BuildApplication()方法的调用,与我们上面所讲WebHost的构建环节具有相同的调用堆栈。而最终返回的正是由中间件衔接而成的RequestDelegate类型代表的请求管道。

4.2. 启动Server

我们先来看下类图:
Here Insert Picture Description
IServer类图

从类图中我们可以看出IServer接口主要定义了一个只读的特性集合属性、一个启动和停止的方法声明。在创建宿主构造器IWebHostBuilder时我们通过调用UseKestrel()方法指定了使用KestrelServer作为默认的IServer实现。其方法申明中接收了一个IHttpApplication<TContext> application的参数,从命名来看,它代表一个Http应用程序,我们来看下具体的接口定义:
Here Insert Picture Description
IHttpApplication类图

The main defines three methods, the first method for creating a request context; second method for processing the request; third method for releasing context. As for the request context, it is used to carry the request and returns the response to the core parameters, which runs through the entire request pipeline and processed. ASP.NET CoreProvides a default implementation HostingApplication, which receives a constructor RequestDelegate _application(i.e. process tubes formed intermediate link) is used to process the request.

var httpContextFactory = _applicationServices.GetRequiredService<IHttpContextFactory>(); var hostingApp = new HostingApplication(_application, _logger, diagnosticSource, httpContextFactory); 
  • 1
  • 2

4.3. StartIHostedService

IHostedServiceInterface is used to define a background task, by implementing this interface and register Iocthe vessel, it will be with ASP.NET Core the program start and start, stop, terminate.

5. Summary

Combined with source code, by ASP.NET Corecombing operation of the call stack, the overall context of its startup process at a glance, and learned that several major key objects:

  1. Responsible for creating the IWebHosthost constructorIWebHostBuilder
  2. On behalf of the host IWebHostinterfaces
  3. Request for constructing the pipelineIApplicationBuilder
  4. Middleware from the convergenceRequestDelegate
  5. Representatives Web Serverof IServerthe interface
  6. Request through the request processing pipeline contextHttpContext
  7. It can be used to register a real service IHostedServiceinterfaces

Guess you like

Origin www.cnblogs.com/nimorl/p/12424101.html