.net core custom middleware

I. Introduction Middleware

  Middleware is assembled to the conduit for the application processing request and response logic of logic components.

  Each component can be:

    1. Choose whether or not the request is passed to the next component in the pipeline.

    2. Before calling the next component in the pipeline and after the execution of the work.

  Delegate requests (Requestdelegates) for constructing the request pipeline, each HTTP request processing.

 

Second, the middleware processing flow

  Logic and Logic out middleware response request comes into execution, the logical order of the incoming register middleware, the opposite logic responsive out.

  Middleware request process by a series of requests and entrusted, as shown below, respectively, the three registered middleware Middleware1, Middleware2, Middleware3. Request will go logic Logic request comes in, and then perform type of next Requestdelegates delegate object (), then the request will go to logic Middleware2 second intermediate, such as black arrows, until a final finish after middleware, will in order Middleware3, Middleware2, Middleware1 response logic to go more logic.

Third, custom middleware

  There are many middleware Microsoft has offered me, but how to customize the middleware it?

The first step: the definition of a class, and add the following code

    public  class Middleware1 
    { 
        Private  Readonly RequestDelegate -next; 

        ///  <Summary> 
        /// pipeline execution request to delegate this intermediate RequestDelegate when a middleware, if other parameters are also obtained by way of injection
         ///  < / Summary> 
        ///  <param name = "Next"> </ param> 
        public Middleware1 (RequestDelegate Next) 
        { 
            // get the object by injection method 
            -next = Next; 
        } 

        ///  <Summary> 
        /// custom middleware logic to perform
         ///  </ Summary> 
        ///  <param name = "context"> </ param> 
        ///  <Returns> </ Returns>
        public the async the Task the Invoke (the HttpContext context) 
        { 
            Console.WriteLine ( " Request1 " ); // request comes logical, sequential order to verify, print a word 
            await -next (context); // pass into the context execution of the next intermediate 
            await context.Response.WriteAsync ( "<P> response1 </ P> " ); // response out logic in order to verify the sequence of the output word 
        } 
    }

Step Two: In the Configure method Startup class, registered by calling the middleware UseMiddleware method. If the self-defined needs middleware Invoke method to pass parameters, the parameters can be passed into the UseMiddleware method, and Middleware1 custom dependency injection is required by way of the parameters passed into the get

        public  void the Configure (App IApplicationBuilder, IHostingEnvironment the env) 
        { 
        // sequentially registered Middleware1, Middleware2, Middleware3. Middleware2, Middleware3 and Middleware1, I would not bother to write a 
            app.UseMiddleware <Middleware1> (); 
            app.UseMiddleware <Middleware2> (); 
            app.UseMiddleware <Middleware3> (); 
        }

Results of the:

 

Guess you like

Origin www.cnblogs.com/yijiayi/p/10964418.html