In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this article, the editor introduces in detail "how to understand ApplicationBuilder in Asp.net 5". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to understand ApplicationBuilder in Asp.net 5" can help you solve your doubts.
ApplicationBuilder (IApplicationBuilder interface), is the basis of OWIN, and it is full of agents, agents, all kinds of lambda expressions, it is estimated that to see this part of the code, a lot of people have to get dizzy.
It is customary to paste the code first (after I modified it, the interface inheritance was removed, and the HttpContext class was modified to its own MyHttpContext class)
Public class ApplicationBuilder {private readonly IList _ components = new List (); public ApplicationBuilder () {} private ApplicationBuilder (ApplicationBuilder builder) {} public ApplicationBuilder Use (Func middleware) {_ components.Add (middleware); return this;} public ApplicationBuilder New () {return new ApplicationBuilder (this) } public RequestDelegate Build () {RequestDelegate app = context = > {context.StatusCode = "404"; System.Console.WriteLine ("404"); return Task.FromResult (0);} Foreach (var component in _ components.Reverse ()) {app = component (app);} return app;}}
RequestDelegate is defined as follows:
Public delegate Task RequestDelegate (MyHttpContext context)
From the ApplicationBuilder source code, we can focus on three points: the _ components, the Use method, and the Build method.
_ components is also an IList object, but the type is a little special-- it is a proxy that takes the proxy RequestDelegate as the parameter and the proxy RequestDelegate as the return value. The proxy can be called a function, that is, the type inside is a function, the parameter of this function is also a function, and the return value is also a function.
The Use method adds a new record after the list object above.
The Build method is to make the _ components array in reverse order into a linked structure (which feels a bit like a linked list). Here are two pictures to illustrate:
Before Build
After Build
We can also see from the code that the parameter of Item1 is given to "404" and the return result is of type RequestDelegate. That is, this return is similar to voidRequestDelegate (MyHttpContext context). If the system gives us a context variable, then the pipe can run from beginning to end. In fact, in Asp.net5, this pipe is used to replace the traditional IHttpModule (which may not be accurate), so now the question is, is the parameter of Item1 the first link or the last link of the pipe? From the graphic point of view, it should be the first link, but in fact this is a misunderstanding. Because both sides of the arrow are parameters and the other is the execution body (the parameter is a method that will be called in the execution body). In the execution body, you may execute the contents of the parameters at the beginning, and then execute the specific contents; or you can execute the specific contents first, then the parameters, and finally part of the specific contents; you can also execute the specific contents first, followed by the parameters; or you may ignore the parameters and go directly to your own contents, so that the previous parameters will be ignored. In other words, regardless of the order, 404 may be the first link of the pipeline, the last link, or the intermediate link, or it may not be implemented at all. This has something to do with the specific writing of Item1, Item2 and so on. (although it is also a chain structure, does it feel different from the linked list?)
Does it feel like too much work? the source code also makes two extension methods for ApplicationBuilder. The code is sorted out as follows:
Public static class RunExtensions {public static ApplicationBuilder Use (this ApplicationBuilder app, Func middleware) {return app.Use (next = > {return context = > {Func simpleNext = () = > next (context); return middleware (context, simpleNext);};}) } public static void Run (this ApplicationBuilder app, RequestDelegate handler) {if (app = = null) {throw new ArgumentNullException ("why?");} if (handler = = null) {throw new ArgumentNullException ("How?");} app.Use (_ = > handler) }}
First of all, let's talk about the Use method. Changing the method is a change to the previous Use method. Change the parameter passed in to Func. What's the advantage of doing this? The previous Func object does not give people a clear sense of clarity, while Func is very clear. The parameter passed in: MyHttpContext is the Context object and Func is the executor of next. The return value is a Task (similar to void). Be manifest at a glance.
As for the Run method, it is obvious that the Run method only executes its own content, not the parameter body. So the chain structure in front of it will be discarded and will not be executed.
Finally, post your own test examples for your reference.
Example 1:
Static void Main (string [] args) {MyHttpContext context = new MyHttpContext () {StatusCode = "A"}; Func middleware = (x, y) = > {context.StatusCode + = "C"; System.Console.WriteLine (context.StatusCode); return y ();}; Func middleware2 = (x, y) = > {context.StatusCode + = "End1"; System.Console.WriteLine (context.StatusCode) Return Task.FromResult (0);}; ApplicationBuilder builder = new ApplicationBuilder (); builder.Use (next = > {return (MyHttpContext o) = > {o.StatusCode + = "B"; System.Console.WriteLine (context.StatusCode) Next (o); return Task.FromResult (0);}); builder.Use (middleware); / / builder.Use (middleware2); / / builder.Use (middleware); / / builder.Run (o = > {o.StatusCode + = "End2" Return Task.FromResult (0);}); builder.Build () .Invoke (context); System.Console.ReadLine ();}
Execution result:
Example 2:
Static void Main (string [] args) {MyHttpContext context = new MyHttpContext () {StatusCode = "A"}; Func middleware = (x, y) = > {context.StatusCode + = "C"; System.Console.WriteLine (context.StatusCode); return y ();}; Func middleware2 = (x, y) = > {context.StatusCode + = "End1"; System.Console.WriteLine (context.StatusCode) Return Task.FromResult (0);}; ApplicationBuilder builder = new ApplicationBuilder (); builder.Use (next = > {return (MyHttpContext o) = > {o.StatusCode + = "B"; System.Console.WriteLine (context.StatusCode) Next (o); return Task.FromResult (0);}); builder.Use (middleware); builder.Use (middleware2); / / builder.Use (middleware); / / builder.Run (o = > {o.StatusCode + = "End2" System.Console.WriteLine (context.StatusCode); return Task.FromResult (0);}); builder.Build (). Invoke (context); System.Console.ReadLine ();}
Execution result:
Example 3:
Static void Main (string [] args) {MyHttpContext context = new MyHttpContext () {StatusCode = "A"}; Func middleware = (x, y) = > {context.StatusCode + = "C"; System.Console.WriteLine (context.StatusCode); return y ();}; Func middleware2 = (x, y) = > {context.StatusCode + = "End1"; System.Console.WriteLine (context.StatusCode) Return Task.FromResult (0);}; ApplicationBuilder builder = new ApplicationBuilder (); builder.Use (next = > {return (MyHttpContext o) = > {o.StatusCode + = "B"; System.Console.WriteLine (context.StatusCode) Next (o); return Task.FromResult (0);}); builder.Use (middleware); / / builder.Use (middleware2); / / builder.Use (middleware); builder.Run (o = > {o.StatusCode + = "End2" System.Console.WriteLine (context.StatusCode); return Task.FromResult (0);}); builder.Build (). Invoke (context); System.Console.ReadLine ();}
Execution result:
After reading this, the article "how to understand ApplicationBuilder in Asp.net 5" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, 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.