In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you what is the registration and construction principle of NET Core middleware, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The emergence of 0x00 problem
Pipe is a key concept in .NET Core, and many important components exist in the form of middleware, including rights management, session management, routing, and so on. So it's important to understand how middleware is registered and eventually built into a pipeline. Many pioneers in the garden have already started the research and study in this area, and they have also written a lot of articles, but I still don't understand some places after reading them. After all, everyone is different, and some content writers think that where common sense does not need to be written, it may be a blind spot for me. Fortunately, the whole project of .NET Core is open source, so I found the source code and looked at it to solve my confusion. At the same time, write a blog record, it is also a supplement, if you can get something from reading it, so much the better. I originally wanted to finish one article, but later I found it was too long, so I divided it into two articles. This is the first article, which mainly talks about the registration of middleware and the construction principle of pipeline. the later one writes about the principles of registering middleware classes and the conventions and features that need to be paid attention to.
Registration of 0x01 middleware
The construction of pipeline mainly includes the registration of middleware and the construction of registered middleware into pipeline. Let's start with the registration of middleware.
What is middleware? To put it simply, middleware is a method that passes in a parameter of type HttpContext and returns Task. The parameter HttpContext contains relevant information such as HTTP request and response, and the middleware can read / modify some of the contents and decide whether to let the next middleware continue to process the HttpContext. This method is wrapped as a delegate RequestDelegate.
In order for the middleware to have the right to decide whether to let the next middleware continue to process HttpContext, the current middleware needs a reference to the next middleware. Therefore, when registering the middleware, you need to register as Func, where the passed parameters refer to the next middleware, return the middleware we registered, and pass in the parameters passed in ApplicationBuilder when you execute the Build () method to build the pipeline (see later). So for ease of understanding, from a less rigorous point of view, middleware is registered in the form of Func and stored in a list.
You can register the middleware through the Use method of ApplicationBuilder
Construction of 0x02 Pipeline
The construction of pipes is to string together Func lists. Take the return value of the first Func as the parameter of the second Func, the return value of the second Func as the parameter of the third Func, and so on. Finally, the RequestDelegate (middleware) returned by the last Func is returned. While the pipe is working, the last RequestDelegate can decide whether to call the penultimate RequestDelegate, and the penultimate RequestDelegate can decide whether to call the penultimate RequestDelegate, and so on until the first RequestDelegate is called. There are two problems:
The first is to build the pipe in this way, and the order of the middleware is the opposite of when it is registered, so the list _ components.Reverse () is first put in the component to ensure the correct order of the middleware.
The second problem is that there is no RequestDelegate to pass in when building the first middleware, so you need to build a middleware with a status code of 404 as the initial RequestDelegate. Of course, the middleware exists at the end of the pipe after the build is completed. In this way, the pipeline construction is complete. The Build () code is as follows:
0x03 test
The pipelines and middleware after construction are shown below:
This is the official picture of Microsoft, which has been quoted in many articles. You can see from this figure that the middleware starts the next middleware by calling next (). If the middleware does not call next (), then all the middleware after it will not be called. One more detail to note is that the call to next () doesn't have to be last. In other words, you can call the later middleware first, and then perform some operations after the latter middleware call is completed (more logic in the figure).
Let's test it separately and build a .NET Core MVC Web project normally.
Test 1: comment out everything in Configure () and register the middleware in turn:
After running, the result is:
This test confirms that the registration order of the middleware seen in the previous code is the calling order.
Test 2: comment out the next call to Middleware1 and leave everything else unchanged. In this way, Middleware1 does not call Middleware2,Middleware2 and all subsequent middleware cannot be called.
The running result is as follows:
This test shows that neither Middleware2 nor Middleware3 is called if Middleware1 does not call next ().
Test 3: modify the Configure () method as follows:
At the beginning, we register a middleware to record the current time, then call all the subsequent middleware, and then calculate the time it takes for all subsequent middleware to execute when we return. In order to look more obvious, a middleware was registered to force sleep for 100 milliseconds. It should be noted that the middleware that forces sleep should be registered before the MVC, because it will be returned directly after the end of the MVC, and the subsequent middleware will not be called.
The running result is:
This test shows that the call to the next middleware does not have to be placed last. You can call the latter middleware first and wait for all the subsequent middleware calls to complete before continuing with the processing.
The above is what the registration and construction principles of .NET Core middleware are, and have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.