My heart ASP.NET Core The new core object WebHost (b)

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

  1. UseKestrel Kestrel set to HttpServer
  2. ConfigureAppConfiguration set up a configuration file
  3. ConfigureLogging configure the logging processor, add two ways Debug and Console
  4. 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) 
}
It determines our request will undergo a final step which is returned to the client. But its life is very short, after all it is just a Builder, after it has completed construction of the pipeline, it's life is over. Build management process is simple:
  • Examples builderFactory acquired from the DI IApplicationBuilderFactory
  • builderFactory.CreateBuilder() 得到 ApplicationBuilder
  • Examples of obtaining IStartupFilter
  • Configure method call IStartupFilter example, transmission at the second step obtained ApplicationBuilder
  • Builder call ApplicationBuilder the method, that is, we are talking about above, all registered Middleware string together a final return RequestDelegate

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

能不能再加几个图呢?比如顺序流程图,帮助新手理解,文章讲的很好,总结的也很棒,感谢

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Guess you like

Origin www.cnblogs.com/owenzh/p/11306965.html