Several ways of OWIN katana registration middleware

Source: Internet
Author: User

first of all, in the startup of the registration of the middleware of the two considerations , see someone write something has misleading effect. About startup Discovery class content, refer to here Http://www.asp.net/aspnet/overview/owin-and-katana/owin-startup-class-detection

1. Using Iapplicationbuilder.user to register middleware is a sequential relationship.

2. The process of executing the registered middleware is as follows: The input initialization is in order, and the output execution is in reverse order.

request occurs -- Initialize middleware 1---> initialize middleware n--and app ignores middleware approach, direct response output ---------> middleware N invoke performs processing -- middleware 1 invoke performs processing ---> Response output

Enter text

The OWIN middleware must have the following code characteristics, either directly in the startup class, or in the middleware class that is written to return the method.

func<idictionary<stringobject// This function has a context dictionary parameter (OWIN environment Dictionary), and returns the task

The idictionary<string in this signature, object> has actually been wrapped in the Katana server layer into a dictionary form request context (HttpContext) Iowincontext, Dictionary values can be accessed using the context property environment

We will register the middleware in this form

Iapplicationbuilder use (func<requestdelegate, requestdelegate> middleware); middleware can be a delegate or  type or instance

Middleware registration into the pipeline, because the middleware returns a task, you can ensure that the pipeline can be processed to find the next task to execute( the next parameter in the code below, Then next is the first known requestdelegate parameter ) to handle the request and response. A task is a middleware that returns a middleware feature.

Now let's look at the first one .

App. Use (new func<requestdelegate, requestdelegate> (next =) (async context ={    Console.WriteLine (" Initialize component start ");     await Next. Invoke (context);    Console.WriteLine (" completion of pipeline execution ") ;));

The above code will output "Initialize component Start" in the console at the time of the request, and when the next step of the component finishes executing, it will print out the "Pipeline execution complete".

Sometimes there are no rules in the component, but you have to accept next as a parameter, but you can ignore it and still need to return a task, so you can write

App. Use (new func<requestdelegate, requestdelegate> (Ignorenext = content=>{    Console.WriteLine ("Therequest ends with me! " );     return Task.fromresult (0);} ));


The second
way to write is to pass an existing method to the delegate. If you have some logic that you need to abstract, but do not want to write a middleware class alone, this is more appropriate.

 Public classstartup{ Public voidConfiguration (Iappbuilder app) {app. Use (NewFunc<requestdelegate, requestdelegate> (next = content=>Invoke (Next, content)); }    Private AsyncTask Invoke (requestdelegate Next, idictionary<string,Object>environment) {Console.WriteLine (The initialization component starts"); awaitNext.        Invoke (Environment); Console.WriteLine ("The pipeline execution is complete"); }}

If you use code to register a component, you must write a real middleware class for a long time.

App. Use (typeof(Loggingmiddleware));

Or you can register with a middleware instance

App. Use (new loggingmiddleware ());

The Third Way is to implement a practical middleware class

 Public classloggingmiddleware{Privaterequestdelegate Next;  PublicLoggingmiddleware (requestdelegate next) { This. Next =Next; }     Public AsyncTask Invoke (idictionary<string,Object>environment) {Console.WriteLine ("Initialize component start"); awaitNext.        Invoke (Environment); Console.WriteLine ("Pipeline Execution Complete"); }}

The Fourth Way we can integrate the Owinmiddleware base class in the Microsoft.owin library is to implement the middleware class. It provides strongly typed access to the Iowincontext.

 Public classloggingmiddleware:owinmiddleware{ PublicLoggermiddleware (Owinmiddleware next):Base(next) {} Public Async OverrideTask Invoke (iowincontext context) {Console.WriteLine ("Initialize component start"); awaitNext.invoke (context); Console.WriteLine ("Pipeline Execution Complete"); }}

The above several ways to achieve all do the same thing, In fact, more complex middleware definitions can be referenced under the Microsoft.AspNet.Diagnostics of several middleware implementations, compared to WelcomePageMiddleware.cs, we are in the app using the Userwelcompage () method to register the middleware.

Several ways of OWIN katana registration middleware

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.