In-depth analysis of ASP. NET Core pipelines (1): pipelines are used to process HTTP requests and core analysis

Source: Internet
Author: User
Tags webhost


In-depth analysis of ASP. NET Core pipelines (1): pipelines are used to process HTTP requests and core analysis


The reason why ASP. NET Core is called a Web development platform is that 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, session, cache, and so on, are also customized to implement the message processing pipeline. 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.


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. We have customized A Message Processing Pipeline Based on the processing process of the HTTP request, so that the received HTTP request messages can flow into the pipeline like water, each part of the pipeline is processed at a time. 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 difficult to understand from the specific implementation point of view. In order to let readers have a deep understanding of this chapter, let's start with the simple section.


1. Starting from Hello World

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 Program consisting of only two types. The Main method as the Program entry is defined in the Program class, and Startup is used as the initialization type. After the program is started, it will be bound to the default port 5000 for HTTP request listening. After any request for the base address "http: // localhost: 5000, the program will respond to "Hello World ".


  1: class Program

  2: {

  3:     static void Main(string[] args)

  4:     {

  5:         new WebHostBuilder()

  6:             .UseKestrel()

  7:             .UseStartup<Startup>

  8:             .Build()

  9:             .Start();

 10:     }

 11: }

 12:  

 13:  

 14: public class Startup

 15: {

 16:     public void Configure(IApplicationBuilder app)

 17:     {

 18:         app.Run(context=>context.Response.WriteAsync("Hello World");

 19:     }

 20: }

This program involves an important object WebHost, which is created through the Build method of WebHostBuilder. WebHost can be regarded as the host of a Web application. Starting a Web application is essentially the host of the Web application. When we call the Start method of WebHost to Start the application, the message processing pipeline for listening, receiving, processing, and responding to HTTP requests is established. In this process, the initialization type registered to WebHostBuilder by calling the UseStartup <T> method will be used to customize this pipeline. In general, the request processing pipeline of ASP. NET Core is built when WebHost is started, and WebHostBuilder is the creator of the latter. The relationship between the three is shown on the right.


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 ASP. the request processing pipeline of NET Core can be customized freely, but the pipeline must have a server, and the server 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, this method is used to register a server named KestrelServer for the pipeline that will be built later.




As the WebHost Start method is called, custom request processing pipelines are constructed according to specific requirements, the server that acts as the first node is bound to a preset port (for example, KestrelServer uses 5000 as the listening port by default) to listen to HTTP requests from the client. Once a request arrives, the server receives the request and standardizes it and forwards it to the subsequent nodes of the pipeline. We convert the request processing node located after the server in the pipeline into "Middleware (Middleware )". Each middleware has its own functions. For example, we have a middleware that specifically implements the routing function, the so-called request processing pipeline customization is embodied in selecting the corresponding middleware as needed to form the final request processing pipeline. The left figure shows the 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. The development framework is basically built on a special middleware. Use ASP. NET Core MVC, the most famous framework as an example, 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 regarded as a part of a middleware. If it must be independent, the entire request processing pipeline will display the structure shown in the right figure.


3. MPS queue Customization

In the demonstrated Hello World Program, we call the extension method UseStartup before calling the Build method of WebHostBuilder to create a WebHost. <T> the method registers a Startup Type of Startup. From the perspective of request processing pipeline, the purpose of the Startup Type of registration is to customize the pipeline to be built. To be more specific, we use this type as the middleware required for pipeline registration. Generally, the registered Startup Type must have a Configure method similar to the following code snippet. This method can be a static method or an instance method. The parameters of this method are not strictly restricted, but the first parameter type must be the IApplicationBuilder interface.


  1: public class Startup

  2: {

  3:     public void Configure(IApplicationBuilder app);

  4: }

The registration of middleware is implemented in such a Configure method. In the demo instance, we call the extension method Run of the IApplicationBuilder interface to register a middleware. The request processing logic it carries is very simple, even if it directly responds to a "Hello World" string. In a real project, we will use ApplicationBuilder to register the corresponding middleware in this method based on the specific application scenario to build a pipeline suitable for the current request processing needs.


  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: }

For example, in an ASP. in the NET Core MVC application, 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. Use pipelines to process HTTP requests

2. Create a "mini version" pipeline to simulate the real pipeline request processing process

3. How does the MPs queue process HTTP requests?

4. How is an MPS queue created?


Reference page:


Http://www.yuanjiaocheng.net/Linq/linq-lambda-expression.html


Http://www.yuanjiaocheng.net/CSharp/Csharp-jagged-array.html


Http://www.yuanjiaocheng.net/CSharp/csharp-action.html


Http://www.yuanjiaocheng.net/ASPNET-CORE/core-user-registration.html


Http://www.yuanjiaocheng.net/CSharp/Csharp-Generics-collection.html


Http://www.yuanjiaocheng.net/Spring/first.html


Http://www.yuanjiaocheng.net/mvc/mvc-helper-Password.html


Http://www.yuanjiaocheng.net/CSharp/Csharp-keys.html


Http://www.yuanjiaocheng.net/mvc/mvc-helper-RadioButton.html


Http://www.yuanjiaocheng.net/Jsp/first.html


Http://www.yuanjiaocheng.net/entity/crud-in-connected.html




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.