. NET Core Request Pipeline

Source: Internet
Author: User
Tags instance method

Excerpt from here

The nature of the HTTP protocol itself determines that any Web application works by listening, receiving, and processing HTTP requests and finally responding to requests, and HTTP request processing is a typical application scenario for piping design. We customize a message processing pipeline based on the processing process of the HTTP request, allowing incoming HTTP request messages to flow into the pipeline like water, making each link of the pipeline a corresponding treatment at a time. The result of the processing is also turned into a message that flows back into the pipeline for processing and eventually translates into an HTTP response to the client.

The HTTP request processing process starts with listening and receiving requests, finally responding to requests, both of which are done by the same object, which we call " server", although the request processing pipeline for ASP. NET core can be freely customized, However, the pipeline must have a server that is the "faucet" of the entire pipeline.

With the invocation of the webhost start method, the request processing pipeline, tailored to the specific requirements, is built, The server that is the first node is bound to a preset port (for example, Kestrelserver uses 5000 as the listening port by default) to start listening for HTTP requests from the client.

Once the request arrives, the server receives and standardizes the request and forwards it to the subsequent nodes of the pipeline, and we turn the request processing node behind the server in the pipeline into " Middleware (middleware)". each middleware has its own independent functions, such as we have a special implementation of the routing function of the middleware, by the implementation of the user authentication, the so-called request processing pipeline customization embodies in accordance with specific requirements to choose the corresponding middleware to make the final processing of the request pipeline. An application built on an ASP. NET core is typically developed based on a framework, and the development framework is basically built on a particular middleware. As an example of the most famous framework of ASP. NET Core MVC, it actually uses a middleware called "routing" to implement the mapping between the request address and the controller/action, and on this basis implements the activation controller, Performs an action and renders a series of features such as view. So the application can be considered as part of a middleware, and if it is necessary to separate it, the entire request processing pipeline will render the structure shown in the image on the right.

From the point of view of the request processing pipeline, the purpose of registering the startup type is to customize the built pipeline, and more specifically, we use this type to register the required middleware for the pipeline. In general, the registered startup type must have a configure method similar to the one shown in the following code fragment, which can be a static method and an instance method. The parameters of this method are not strictly limited, but the first parameter type must be a 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 a real project, we will use Applicationbuilder to register the appropriate middleware in such a way based on the specific application scenario and build a pipeline that fits the current request processing requirements.

1  Public classStartup2 {3      Public voidConfigure (Iapplicationbuilder app)4     {5App. Useexceptionhandler ("/home/error");6 app. Usestaticfiles ();7 app. Useidentity ();8 9 app. Usemvc ();Ten     } One}

For example, in an ASP. NET Core MVC application, in addition to calling the extension method as described above, USEMVC registers the middleware that supports the MVC framework (which is actually a middleware that implements routing). We have also registered the corresponding middleware by calling other extension methods to implement the access to the static file (Usestaticfiles), the rendering of the error page (Useexceptionhandler), and the ASP-based Certification of the framework (useidentity).

. NET Core Request Pipeline

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.