Registration of. NET Core middleware and pipeline construction (3), core Middleware

Source: Internet
Author: User

Registration of. NET Core middleware and pipeline construction (3), core Middleware
. NET Core middleware registration and pipeline construction (3) ---- use Map/MapWhen Extension Method 0x00 why Map (MapWhen) extension is required

If the business logic is relatively simple, a main pipeline is enough and Map is not used. However, when the business logic is complex, we may want to use a special set of middleware to process HttpContext based on different situations. In this case, if only one pipeline is used, the processing will be very troublesome and confusing. In this case, you can use Map/MapWhen to create a branch pipeline. When the conditions meet our settings, the branch channel handles HttpContext. It is easy to add a branch pipeline by using Map/MapWhen. You only need to provide the judgment logic suitable for jump to the branch pipeline and the construction method of the branch pipeline.

0x01 principle of Map Extension

As we mentioned in the previous article, the middleware registration and pipeline construction are both carried out through ApplicationBuilder. Therefore, to build a branch pipeline, you need a new ApplicationBuilder to register the middleware and build the pipeline. In order to share the services we registered in the current ApplicationBuilder in the branch pipeline (or share the dependency injection container, of course, more than this is shared ), when creating a new ApplicationBuilder, instead of directly creating a New one, it calls the new method of the current ApplicationBuilder to create a New one based on the current one, shares the Properties of the current ApplicationBuilder (including the dependency injection container ).

When using Map to register middleware, we will pass in an Action <IApplicationBuilder> parameter, which is used to set the new ApplicationBuilder, the most important thing is to register the middleware of the branch pipeline on the new ApplicationBuilder. After the configuration is complete, call the Builder method of the branch ApplicationBuilder to build the pipeline, and save the first middleware as the ingress of the branch pipeline.

When using Map to register middleware, A PathString parameter is input. The PathString object can be considered as a string. It is used to record the segments (Segment) to be matched in HttpContext. HttpRequest. Path ). The end of this string parameter cannot be "/". If the match is successful, the branch MPs queue is displayed. If the match is lost, the current MPs queue is resumed.

The newly built pipeline and the matching string are saved as MapOptions objects, and the Map rules and the ingress of the branch pipeline are saved. Then construct the MapMiddleware object and wrap the Invoke method as RequestDelegate. Use the Use method of the current ApplicationBuilder to register the middleware.

The following is the Map extension method:

The following is the Invoke method of MapMiddleware.

0x02 principle of MapWhen Extension

Map mainly uses the Path in the URL to determine whether to enter the branch pipeline, but sometimes we may have other requirements. For example, I want to process all the requests whose Method is DELETE using a special pipeline. MapWhen is required at this time. MapWhen is a common Map. Users can decide when to enter the branch pipeline and when not. It can be said that Map is a situation of MapWhen, because this situation is too common, so the official implementation of. In this case, MapWhen is simple. In Map, we pass in the PathString parameter for HttpRequest. path match. In MapWhen, we pass in the Func <HttpContext, bool> parameter, which we specify. When true is returned, the pipeline is added to the Branch. If false is returned, the current pipeline continues.

The following is the MapWhen extension method:

 

The following is the Invoke method of MapWhenMiddleware:

0x03 By The Way, Run Extension

Another Extension Method for registering middleware is Run, which is very simple. Run registers a middleware with Use, and returns directly without calling the next middleware. Therefore, using the Run Extension Method to register the middleware will always be directly returned, and subsequent middleware will not be called.

0x04 written at the end

Map, MapWhen, and Run are both simple. So far, the middleware registration and pipeline building topics have been completed. At the beginning, I started out of some confusion about middleware. After I understood it, I wanted to write a blog and share my experiences. I didn't expect to write more and more, one article is divided into two articles. Up to now, I have written three articles. I just wrote some rough information and did not write many details. If you are interested, you can go to github and check the code. Pipeline and middleware are all in this repository: https://github.com/aspnet/http?actions.

0x05 related articles

Registration of. NET Core middleware and pipeline construction (1) ---- registration and Construction Principles

Registration of. NET Core middleware and pipeline construction (2) ---- register the middleware class using UseMiddleware Extension Method

Registration of. NET Core middleware and pipeline construction (3) ---- use the Map/MapWhen Extension Method

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.