Registration and pipeline construction for. NET Core Middleware (3)----using the Map/mapwhen extension method

Source: Internet
Author: User

Registration and pipeline construction for. NET Core Middleware (3)----using the Map/mapwhen extension method 0x00 why a map (mapwhen) extension is required

If the business logic is simple, a main pipeline is enough, and it does not have a map. But when business logic is more complex, sometimes we might want to use a special set of middleware to handle HttpContext, depending on the situation. In this case, if you use only one pipe, it can be very cumbersome and confusing to handle. At this point, you can use Map/mapwhen to create a branch pipeline that is handled by this branch pipeline when the condition conforms to our settings HttpContext. It is easy to add branch pipelines using Map/mapwhen, as long as you provide the appropriate decision logic to jump to the branch pipeline, as well as the branching pipeline construction method.

The principle of the 0x01 map extension

As we said in the previous article, the registration of middleware and the construction of pipelines are carried out through Applicationbuilder. So to build a branch pipeline, you need a new applicationbuilder and use it to register the middleware and build the pipeline. In order to be able to share the services we have registered in the current Applicationbuilder in the branch pipeline (or share the dependency injection container, of course, there is more than that), when creating a new applicationbuilder, it is not just new, Instead, the new method of the current applicationbuilder is called to create the current base, which shares the properties of the current Applicationbuilder (which contains the dependency injection container).

When we use the map to register the middleware, we pass in a action<iapplicationbuilder> parameter, which is what it does when we create a new Applicationbuilder and use this method to set it up. The most important thing is to register the middleware for 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 portal for the branch pipeline.

When using map to register the middleware, a pathstring parameter is passed in, and the PathString object can be simply considered a string. It is used to record the segment (Segment) to match in the HttpContext.HttpRequest.Path. The end of this string argument cannot be "/". If the match succeeds, it enters the branch pipeline, and the match loses to continue the current pipeline.

The newly constructed pipeline and the string used for matching are saved as mapoptions objects, with the map rules and the portal of the branch pipeline saved. After that, the Mapmiddleware object is constructed and its invoke method is wrapped as requestdelegate, and the middleware is registered using the current Applicationbuilder use method.

Here is the map extension method:

Here is the Invoke method for Mapmiddleware

The principle of 0x02 mapwhen extension

The map mainly uses path in the URL to determine if it needs to go into the branch pipeline, but sometimes we may have other needs, such as I want to use a special pipeline for all method delete requests. This is the time to use Mapwhen. Mapwhen is a generic map that can be used by users to decide when to enter a branch pipe when not to enter. It can be said that map is a case of mapwhen, because this situation is too common, so the official implementation of one. So it seems Mapwhen is very simple, in the map we passed the parameter pathstring to do httprequest.path matching, in Mapwhen we passed the func

Here is the Mapwhen extension method:

Here is the Invoke method for Mapwhenmiddleware:

0x03 by the way, the run extension

There is also a way to expand the registration middleware is run, because it is very simple, so in the last mention. Run is to register a middleware with use, and then return directly without invoking the next middleware. So registering middleware with the run extension method is always returned directly, and the middleware behind it is not called.

0x04 written in the last

Map/mapwhen and run are very simple and do not write tests. So far, the topic of the registration of middleware and the construction of pipelines is finished. The beginning is out of the middleware in some places of confusion began, and understand after this thought to write a blog share under their own experience, did not think more write more, a piece divided into two, to now write three. I've just written about it, a lot of the details are not written, interested can go to GitHub to look at the code, pipeline and middleware related in this warehouse: Https://github.com/aspnet/HttpAbstractions.

Registration and pipeline construction for. NET Core Middleware (3)----using 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.