This is the new ASP.NET Core core object the second in the series, on a WebHost preparation phase we talked about the initialization and configuration of WebHostBuilder. We make the following configuration to WebHostBuilder
- UseKestrel Kestrel set to HttpServer
- ConfigureAppConfiguration set up a configuration file
- ConfigureLogging configure the logging processor, add two ways Debug and Console
- UseStartup specified Startup class
Once you have a WebHostBuilder, we can call it the Build method to construct a WebHost.
We can first look at the code method Build
// 初始化DI
var hostingServices = BuildCommonServices(out var hostingStartupErrors);
var applicationServices = hostingServices.Clone();
var hostingServiceProvider = hostingServices.BuildServiceProvider();
// 构造 WebHost
var host = new WebHost(
applicationServices,
hostingServiceProvider,
_options,
_config,
hostingStartupErrors);
// 初始化host
host.Initialize();
return host;
BuildCommonServices
This is the first method WebHostBuild method of execution, mainly new ServiceCollection and the completion of the registration number of the following basic examples:
- IApplicationBuilderFactory
- IHttpContextFactory
- IMiddlewareFactory
- IStartupFilter
- IServiceProviderFactory <IServiceCollection>
- start-ups
In fact, the IStartup registered to DI, the use of two ways. If we specified in WebHostBuilder in Startup.cs inherited from IStartup interface directly bound to IStartup.
if (typeof(IStartup).GetTypeInfo()
.IsAssignableFrom(startupType.GetTypeInfo()))
{
services.AddSingleton(typeof(IStartup), startupType);
}
Otherwise, the need to follow the naming convention to construct a Startup.
services.AddSingleton(typeof(IStartup), sp =>
{
var hostingEnvironment =
sp.GetRequiredService<IHostingEnvironment>();
var methods = StartupLoader.LoadMethods(
sp,
startupType,
hostingEnvironment.EnvironmentName);
return new ConventionBasedStartup(methods);
});
Host initialization
Host initialization is time we talk about the focus, because it completed the construction of the entire Http pipeline.
if (_application == null)
{
_application = BuildApplication();
}
This _application that is a RequestDelegate. Argument is a HttpContext.
public delegate Task RequestDelegate(HttpContext context);
Let's look at BuildApplication approach has seven steps, five steps back and sum up IAapplicationBuilder is constructed using Configure a method of constructing Startup.cs we speak above this RequestDeleaget.
IApplicationBuilder
IApplicationBuilder defined in HttpAbstractions this project which, HttpAbstractions ASP.NET Core is a sub-project on Githab, it is also an important part of ASP.NET Core, which defines the behavior of ASP.NET Core lot about HTTP handle the entire process. Even if a IApplicationBuilder.
It mainly includes several important attributes:
Configure the way we see in Startup.cs parameter is given in this IApplicationBuilder, and this method is used to add the Middleware.
public void Configure(IApplicationBuilder app) {
// 相当于 List<Middleware>().add(某个middleware)
app.Use(某个Middleware)
}
WebHost.Start
After our WebHost initialization is complete, it will be called the Start method to start the port monitor IServer and begin processing Http request. This is the third stage of WebHost: Start the process. We will describe in next Thursday's 20:00.
1 条评论
Allen · 2018年2月27日 下午2:59
能不能再加几个图呢?比如顺序流程图,帮助新手理解,文章讲的很好,总结的也很棒,感谢