ASP. NET core Pipeline depth profiling (1): Handling HTTP requests with pipelines

Source: Internet
Author: User
Tags webhost

The reason that ASP. NET core is a Web development platform is that it has a highly extensible request processing pipeline that we can customize to meet the HTTP processing needs of various scenarios. Asp. NET core applications, such as routing, authentication, sessions, caching, and so on, also customize the message processing pipeline to achieve. We can even create our own web framework on the ASP. In fact, the two important web frameworks of MVC and SINGALR are created in this way.

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 message processing pipeline for ASP. NET core is very simple from the point of view of design, but it is relatively difficult to understand from the point of view of concrete implementation, in order to let readers have a deep understanding of this chapter, we start from the simple part.

first, speaking from Hello world

To enable readers to understand the message processing pipeline of ASP. NET core with the most intuitive experience, we create a simple Hello World program. This is a console program consisting of only two types, which is defined as the main method in the program class, and startup is the initialization type. After this program is started, it will be bound to the default port 5000 for HTTP request monitoring, and any request for base site "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 ();
  Ten:     }
One   : }
  12:
  13:  
  :publicclass Startup
  : {
  :      Public void Configure (Iapplicationbuilder app)
  :     {
  :         app. Run (Context=>context. Response.writeasync ("Hello World");
  :     }
  : }

This program involves an important object webhost, which is created by the Webhostbuilder build method. Webhost can be seen as a host of Web applications, and launching a web app is essentially the host that launches it. The message processing pipeline for listening, receiving, processing, and responding to HTTP requests is built when we invoke the Webhost start method to launch the application. In this process, the initialization type registered to Webhostbuilder by calling the Usestartup<t> method will be used to customize the pipeline. In general, the request processing pipeline for ASP. NET core is built by webhost at startup, and Webhostbuilder is the creator of the latter, and the diagram on the right reveals the relationship between the three.

Ii. composition of the pipeline

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. In this Hello World application above, before calling Webhostbuilder's build method to create a webhost, we called an extension method Usekestrel, The function of this method is to register a server named Kestrelserver for the subsequent build 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. The diagram on the left shows the request processing pipeline consisting of a server and a set of middleware.

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.

third, the pipeline customization

In the presentation of the Hello World program, we called its extension method before calling Webhostbuilder's Build method to create webhost usestartup<t> Method registers a startup type that is type startup. From the point of view of the request processing pipeline, the purpose of the registered 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:publicclass 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 of the Iapplicationbuilder interface run to register a middleware that hosts the request processing logic very simply, even if it responds directly to a "Hello world" string. 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:publicclass Startup
   2: {
   3:      Public void Configure (Iapplicationbuilder app)
   4:     {
   5:         app. 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).

I. Using pipelines to process HTTP requests
Second, create a "mini-version" of the pipeline to simulate the real pipeline request processing process
Third, how the pipeline handles HTTP requests
Iv. how the pipeline was 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

ASP. NET core Pipeline depth profiling (1): Handling HTTP requests with pipelines

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.