Quote :
1 Introduction
For ASP.NET Core
applications, 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 Core
cross-platform Cornerstone). ASP.NET Core
Has 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 Core
applications configure and start a Host
management application to complete the startup, and their life cycle (as shown below). The Host
main duty is Web Server
disposed and Pilpeline
(request processing pipeline) construct.
This figure depicts an overall boot process, we know from the image above ASP.NET Core
to launch the application consists of three main steps:
CreateDefaultBuilder()
:createIWebHostBuilder
Build()
:IWebHostBuilder
Responsible for creatingIWebHost
Run()
:start upIWebHost
So, ASP.NET Core
the start of the essence of the application is to start as a host of WebHost
objects.
Which is mainly related to two key objects IWebHostBuilder
and IWebHost
their internal implementation is ASP.NET Core
the core of the application. Here we combine combing the call stack and source code to find out!
2. Host Constructor:IWebHostBuilder
In the start IWebHost
before the host, we need to complete IWebHost creation and configuration. And this work need to use IWebHostBuilder
objects to complete, ASP.NET Core
provides a default implementation WebHostBuilder
. And WebHostBuilder
by WebHost
the same name tools ( Microsoft.AspNetCore
in the namespace) of the CreateDefaultBuilder
method of creation. CreateDefaultBuilder()
Call stack
从上图中我们可以看出CreateDefaultBuilder()
方法主要干了六件大事:
UseKestrel
:使用Kestrel
作为Web server
。UseContentRoot
:指定Web host使用的content
root(内容根目录),比如Views。默认为当前应用程序根目录。ConfigureAppConfiguration
:设置当前应用程序配置。主要是读取appsettinggs.json
配置文件、开发环境中配置的UserSecrets
、添加环境变量和命令行参数 。ConfigureLogging
:读取配置文件中的Logging节点,配置日志系统。UseIISIntegration
:使用IISIntegration
中间件。UseDefaultServiceProvider
:设置默认的依赖注入容器。
创建完毕WebHostBuilder
后,通过调用UseStartup()
来指定启动类,来为后续服务的注册及中间件的注册提供入口。
3. 宿主:IWebHost
在ASP.Net Core中定义了IWebHost
用来表示Web
应用的宿主,并提供了一个默认实现WebHost
。宿主的创建是通过调用IWebHostBuilder
的Build()
方法来完成的。那该方法主要做了哪些事情呢,我们来看下面这张【ASP.NET Core启动流程调用堆栈】中的黄色边框部分:
ASP.NET Core启动流程调用堆栈
其核心主要在于WebHost
的创建,又可以划分为三个部分:
- 构建依赖注入容器,初始通用服务的注册:
BuildCommonService();
- 实例化
WebHost
:var host = new WebHost(...);
- 初始化
WebHost
,也就是构建由中间件组成的请求处理管道:host.Initialize();
3.1. 注册初始通用服务
BuildBuildCommonService
方法主要做了两件事:
- 查找
HostingStartupAttribute
特性以应用其他程序集中的启动配置 - 注册通用服务
- 若配置了启动程序集,则发现并以
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. 构建请求处理管道
请求管道的构建,主要是中间件之间的衔接处理。
而请求处理管道的构建,又包含三个主要部分:
- 注册
Startup
中绑定的服务; - 配置
IServer
; - 构建管道
请求管道的构建主要是借助于IApplicationBuilder
,相关类图如下:
请求管道的构建
4. 启动WebHost
WebHost
的启动主要分为两步:
- 再次确认请求管道正确创建
- 启动
Server
以监听请求 - 启动
HostedService
启动WebHost
调用堆栈
4.1. 确认请求管道的创建
从图中可以看出,第一步调用Initialize()
方法主要是取保请求管道的正确创建。其内部主要是对BuildApplication()
方法的调用,与我们上面所讲WebHost
的构建环节具有相同的调用堆栈。而最终返回的正是由中间件衔接而成的RequestDelegate
类型代表的请求管道。
4.2. 启动Server
我们先来看下类图:IServer
类图
从类图中我们可以看出IServer
接口主要定义了一个只读的特性集合属性、一个启动和停止的方法声明。在创建宿主构造器IWebHostBuilder
时我们通过调用UseKestrel()
方法指定了使用KestrelServer
作为默认的IServer
实现。其方法申明中接收了一个IHttpApplication<TContext> application
的参数,从命名来看,它代表一个Http
应用程序,我们来看下具体的接口定义: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 Core
Provides 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
IHostedService
Interface is used to define a background task, by implementing this interface and register Ioc
the vessel, it will be with ASP.NET Core
the program start and start, stop, terminate.
5. Summary
Combined with source code, by ASP.NET Core
combing operation of the call stack, the overall context of its startup process at a glance, and learned that several major key objects:
- Responsible for creating the
IWebHost
host constructorIWebHostBuilder
- On behalf of the host
IWebHost
interfaces - Request for constructing the pipeline
IApplicationBuilder
- Middleware from the convergence
RequestDelegate
- Representatives
Web Server
ofIServer
the interface - Request through the request processing pipeline context
HttpContext
- It can be used to register a real service
IHostedService
interfaces