Through the reconstruction of the Hosting system, we can understand the HTTP request processing process in the ASP. NET Core pipeline [Up]: The pipeline is used to process the request, and the hostingcore

Source: Internet
Author: User
Tags webhost

Through the reconstruction of the Hosting system, we can understand the HTTP request processing process in the ASP. NET Core pipeline [Up]: The pipeline is used to process the request, and the hostingcore

It is called ASP. NET Core is a Web development platform, rather than a simple development framework, because it has a highly scalable request processing pipeline, we can customize this pipeline to meet HTTP processing needs in various scenarios. Many features of ASP. NET Core applications, such as routing, authentication, sessions, and caching, are achieved through custom pipelines. We can even use pipelines to create our own Web framework on the ASP. NET Core platform. In fact, the two important Web frameworks MVC and SingalR are also created in this way. This article has been synchronized to ASP. NET Core framework secrets.]

 

Directory
1. Starting from Hello World
Ii. Pipeline Composition
3. MPS queue Customization

1. Starting from Hello World

The characteristics of the HTTP protocol determine that the way any Web application works is to listen, receive, and process HTTP requests, and finally respond to the requests, HTTP request processing is a typical scenario of pipeline design. Specifically, we construct an Pipeline Based on the specific HTTP Processing request. The received HTTP Request Message flows into this pipeline like water, and each step of this pipeline is processed accordingly. The processing result is also converted into a message reversely flowing into the pipeline for processing, and finally into an HTTP Response to the client. ASP. the message processing pipeline of NET Core is very simple from the design point of view, but it is relatively complicated and difficult to understand from the specific implementation point of view, to give readers a deep understanding of this chapter, let's start with the simple section.

To enable readers to understand the message processing pipeline of ASP. NET Core with the most intuitive experience, we will create a simple Hello World Program. This is a console application. The entire program consists of the following five lines of code. After we run this program, a server named KestrelServer will start and bind it to port 5000 on the local machine for request listening. For all received requests, we will respond to a "Hello World" string.

   1: public class Program
   2: {
   3:     public static void Main()
   4:     {
   5:         new WebHostBuilder()
   6:             .UseKestrel()
   7:             .Configure(app => app.Run(async context=> await context.Response.WriteAsync("Hello World")))            
   8:             .Build()
   9:             .Run();
  10:     }
  11: }

This program involves an important object named WebHost, which can be viewed as the Web application host. Starting a Web application is essentially a WebHost object that starts as the host. WebHostBuilder is the creator of WebHost. We call its Build method to create the corresponding WebHost. When we call the extension method Run of WebHost to start the application, the pipeline for listening, receiving, processing, and responding to HTTP requests is established. In this process, the delegate object registered to WebHostBuilder by calling the Configure method (the delegate type is Action <IApplicationBuilder>) will be used for pipeline customization. In general, the ASP. NET Core pipeline is built when WebHost is started, and WebHostBuilder is the creator of the latter, revealing the relationship between the three.

Ii. Pipeline Composition

The HTTP request processing process starts with listening and receiving requests, and finally responding to requests. These two tasks are completed by the same object, which is called "Server )". Although the request processing pipeline of ASP. NET Core can be customized freely, the pipeline must have a Server, which is the "Leader" of the entire pipeline ". In the preceding Hello World application, before calling the WebHostBuilder Build method to create a WebHost, we call UseKestrel, the function of this method is to register a Server named KestrelServer for the pipeline that will be built later.

With the WebHost Start method (when we call the WebHost Extension Method Run, its Start method will be automatically called) called, the custom pipeline will be built, the server of the MPs queue is bound to a preset port (for example, KestrelServer uses 5000 as the listening port by default) to start listening for requests. Once an HTTP request arrives, the Server will standard it and distribute it to subsequent nodes of the pipeline. We call the node located after the Server in the pipeline "Middleware (Middleware )". Each middleware has its own functions. For example, we have a middleware dedicated to routing and a middleware dedicated to implementing user authentication. The so-called pipeline customization is embodied in selecting the corresponding middleware as needed to form the final pipeline for request processing. Reveals a request processing pipeline composed of a server and a set of middleware.

An application built on ASP. NET Core is generally developed based on a framework. Generally, the development framework itself is built through one or more middleware. Use ASP. NET Core MVC, the most famous development framework, is used as an example. It uses a middleware called "routing" to realize the ing between the request address and the Controller/Action, in addition, the following functions are implemented: Activating the Controller, executing the Action, and displaying the View. Therefore, an application can be considered as a part of a middleware. If it must be independent, the entire request processing pipeline will display the structure shown in.

3. MPS queue Customization

In the demonstrated Hello World Program, after we call the extended method UseKestrel to register the KestrelServer server, webHostBuilder is also called. For example, the next extension method named Configure registers a delegate object of the Action type <IApplicationBuilder>. From the perspective of the request processing pipeline, the purpose of registering this delegate object is to customize the constructed pipeline. To be more specific, we use this type as the middleware required for pipeline registration. This delegate object registered in the demo instance calls the extended method of ApplicationBuilder Run and registers a middleware to respond to each request with a "Hello World" string.

   1: public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action<IApplicationBuilder> configureApp) 

In addition to registering an Action <IApplicationBuilder> type delegate by calling the Configure method of WebHostBuilder, the logic for registering intermediate definition pipelines is more defined in a separate type. Because MPs queue customization is always performed when the application starts (Startup), we generally call this MPs queue as the Startup type ", in most cases, it is directly named as Startup. According to the Conventions, the custom pipeline operation by registering the middleware will be implemented in the Configure method. The first parameter type of the method must be the IApplicationBuilder interface, and any number of parameters and types can be defined later, when this method is ASP. when the NET Core framework is called, these parameters are provided using dependency injection. The START type can be registered by calling the extension method UseStartup <T> of WebHostBuilder. The following code is exactly the same as the preceding example.

   1: public class Program
   2: {
   3:     public static void Main()
   4:     {
   5:         new WebHostBuilder()
   6:             .UseKestrel()
   7:             .UseStartup<Startup>()
   8:             .Build()
   9:             .Run();
  10:     }
  11:     public class Startup
  12:     {
  13:         public void Configure(IApplicationBuilder app)
  14:         {
  15:             app.Run(async context => await context.Response.WriteAsync("Hello World"));
  16:         }
  17:     }
  18: }

In real project development, we will use ApplicationBuilder to register the corresponding middleware to build a pipeline suitable for the current request processing needs. As shown in the following code snippet, in addition to calling the extension method UseMvc as shown above, we registered the Middleware supporting the MVC Framework (actually a routing-based middleware, we also registered the corresponding middleware by calling other extension methods to achieve access to static files (UseStaticFiles), error page rendering (UseExceptionHandler), and ASP-based.. NET Identity Framework (UseIdentity ).

   1: public class Startup
   2: {
   3:     public void Configure(IApplicationBuilder app)
   4:     {
   5:         app.UseExceptionHandler("/Home/Error");
   6:         app.UseStaticFiles();
   7:         app.UseIdentity();           
   8:  
   9:         app.UseMvc();
  10:     }
  11: }

 

Using the Hosting system to understand the HTTP request processing process in the ASP. NET Core pipeline [I]: Using pipelines to process requests
Rebuild the Hosting system to understand the HTTP request processing process in the ASP. NET Core pipeline [medium]: How the pipeline processes the request
Rebuild the Hosting system to understand the HTTP request processing process in the ASP. NET Core pipeline [bottom]: How to Create a pipeline

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.