Asp.net core middleware principle and usage Commentary

Asp.net core middleware principle and usage Commentary

 

Brief asp.net core realization of ideas Middleware

 

A http process request is for a Request request logic processing several times, and eventually disposed Response process. From realize the dimension of the code, due to Request and Response are HttpContext years, this process can be represented as "to a httpContext as a function of the input of the commission," that delegate Task RequestDelegate (HttpContext context) describe, for the convenience of this article, we temporarily delegate this function is called "request processing logic."

 

 

The role of middleware is behind a request to join a processing logic, the processing logic is "before a request processing logic" as the input, middleware and after their treatment, returned a "new request processing logic." Therefore, from the code may be "middleware" is entrusted to a table type "request processing logic" as input and returns another "request processing logic", i.e. Func <RequestDelegate, RequestDelegate> . The multiple middleware that is expressed as List <Func <RequestDelegate, RequestDelegate >> .

 

 

Asp.net core intermediate the core function of how a series of middleware, combined into a "request processing logic" process, i.e., how List <Func <RequestDelegate, RequestDelegate >> combined to generate a RequestDelegate . Merge logic follows

 

 

There are two places above code to note

1, asp.net core default last request to join a " 404 " middleware processing.   

2, when the combined components are recycled to the reverse order

   Due to join the middleware must be executed, so in the merger, the first middleware to finally merge, that is, to reverse the cycle after the merger middleware

 

How to use middleware

Middleware There are four ways: Use , the Run , the Map and the use of Middleware class , but the first three methods are the final call Use methods, we take a look Use to achieve a logical approach, as follows

 

 

That is, use the method only in the middleware list ( _components ) is then add a middleware

 

Use the following four methods are described in detail

 

Use usage

Ues used in two ways

Use a

Call IApplicationBuilder Use (Func <RequestDelegate, RequestDelegate > middleware), this usage to be in middleware its delegate control their own whether to enter the next middleware, and to create and own a RequestDelegate return, the wording would be more complicated.

Examples are as follows

 

Usage of Two

Call IApplicationBuilder Use (this IApplicationBuilder app, Func <HttpContext, Func <Task>, Task> middleware), which is an extension method, which do not have to create RequestDelegate and return, the wording more concise. The final method call it a realization or usage of this method codes are as follows.

 

Examples are as follows

 

 

Please note: the above two Use usage, while the second middleware, and no longer call the next middleware, which is to ensure that the http request does not enter into the asp.net core default last 404 middleware, because the last 404 middleware set status code , but rather a response body when previously start writing, is no longer change the status code , or request header , otherwise it will error. Microsoft's official documentation requires the use of middleware to the following rules: If the response body change under after a call not to middleware, to avoid the next middleware on a middleware httpcontext pollution content. (This article is an example for demonstration purposes, did not follow this convention)

 

 

 

run usage

run process codes are as follows

 

Note: From the run can be seen in the code for a method, run is no longer perform the next middleware, so the first intermediate run behind middleware approach does not work. It is generally used run time are placed at the end of middleware

 

 

map usage

map actually is not exact middleware usage, but to open a "middleware request route branch" in the "branch" where you can then use and run methods to a new middleware component logic.

Examples are as follows

 

As an example, when the request matches the address / test , the only enabled map inside middleware

 

Middleware class usage

Middleware class does not need to extend any classes or interfaces, but there must be called the Invoke , the return type of the Task , and the first parameter HttpContext types.

Examples are as follows

 

 

 

 

Asp.net core built-in middleware Introduction

 

Middleware name

How to use and instructions

Authentication

App.UseAuthentication, verify the user's current request, and set the HttpContext.User , when the time OAuth callbacks, will suspend the execution of the next intermediate. Middleware placed in front of the user authentication to use

Static File

app.UseStaticFiles (), to determine whether the current request is a static file, if it is then suspend the execution of the next middleware, or continue to the next middleware. Into the pipeline foremost

Response Caching

app.UseResponseCaching (), caching middleware

MVC

app.UseMvc (), the MVC is introduced into the intermediate pipe, if the requested address to find the corresponding MVC route, then suspend the execution of the next intermediate. Into the final pipeline.

Exception

app.UseDeveloperExceptionPage (); or app.UseExceptionHandler (); abnormality information processing program. Into the pipeline foremost

Authorization 

Authorized Middleware. Without direct reference, App.UseMvc () will be called internally, and with app.UseAuthentication () used together.

 

 

Middleware summary

1, through the use, run, map, middleware class using four methods

2, when a plurality of middleware, the middleware should pay attention to the order

3 , in the design of middleware, please follow the "separation of duties" principle, that only a middleware "sole responsibility" for processing, such as user verification and authorization.

4 , if response body after made changes, please do not perform the next middleware

 

 

 

Guess you like

Origin www.cnblogs.com/shengyu-kmust/p/11583974.html