How to run Owin Middleware in the IIS integration pipeline, owinmiddleware

Source: Internet
Author: User

How to run Owin Middleware in the IIS integration pipeline, owinmiddleware

Owin Middleware Components (OMCs)

InstallInstall-Package Microsoft.Owin.Host.SystemWeb

Allows OMCs to work under the IIS integration Pipeline

In the IIS integration pipeline, this request pipeline includes HttpModules associated with a set of predefined pipeline events, such

BeginRequest, AuthenticateRequest, AuthorizeRequest, etc.

If we compare OMC with HttpModule, OMC must be registered to an appropriate predefined pipeline event like HttpModule. For example, the following Httpmodule,

When a request comes to the AuthenticateRequest stage,MyModuleWill be called

public class MyModule : IHttpModule{    public void Dispose()    {        //clean-up code here.    }    public void Init(HttpApplication context)    {        // An example of how you can handle AuthenticateRequest events.        context.AuthenticateRequest += ctx_AuthRequest;    }    void ctx_AuthRequest(object sender, EventArgs e)    {        // Handle event.    }}

To enable the OMC to take the same event-based execution sequence as the HttpModule, Katana runs the time code scan Startup configuration and Associates each OMC to an integrated pipeline event,

For example, the following Configuration:

using System;using System.Threading.Tasks;using Microsoft.Owin;using Owin;using System.Web;using System.IO;using Microsoft.Owin.Extensions;[assembly: OwinStartup(typeof(owin2.Startup))]namespace owin2{    public class Startup    {        public void Configuration(IAppBuilder app)        {            app.Use((context, next) =>            {                PrintCurrentIntegratedPipelineStage(context, "Middleware 1");                return next.Invoke();            });            app.Use((context, next) =>            {                PrintCurrentIntegratedPipelineStage(context, "2nd MW");                return next.Invoke();            });             app.Run(context =>            {                PrintCurrentIntegratedPipelineStage(context, "3rd MW");                return context.Response.WriteAsync("Hello world");            });                    }        private void PrintCurrentIntegratedPipelineStage(IOwinContext context, string msg)        {            var currentIntegratedpipelineStage = HttpContext.Current.CurrentNotification;            context.Get<TextWriter>("host.TraceOutput").WriteLine(                "Current IIS event: " + currentIntegratedpipelineStage                + " Msg: " + msg);        }    }}

The output is as follows:

Current IIS event: PreExecuteRequestHandler Msg: Middleware 1Current IIS event: PreExecuteRequestHandler Msg: 2nd MWCurrent IIS event: PreExecuteRequestHandler Msg: 3rd MW

We can see that each OMC is mapped to the IIS pipeline event PreRequestHandlerExecute by default during Katana runtime.

You can adjust the default relationship between the OMC and the MPs queue event as needed. Use an extension method.IAppBuilder UseStageMarker(),

As shown below:

public void Configuration(IAppBuilder app){    app.Use((context, next) =>    {        PrintCurrentIntegratedPipelineStage(context, "Middleware 1");        return next.Invoke();    });    app.Use((context, next) =>    {        PrintCurrentIntegratedPipelineStage(context, "2nd MW");        return next.Invoke();    });    app.UseStageMarker(PipelineStage.Authenticate);    app.Run(context =>    {        PrintCurrentIntegratedPipelineStage(context, "3rd MW");        return context.Response.WriteAsync("Hello world");    });    app.UseStageMarker(PipelineStage.ResolveCache);}

The output is as follows:

Current IIS event: AuthenticateRequest Msg: Middleware 1Current IIS event: AuthenticateRequest Msg: 2nd MWCurrent IIS event: ResolveRequestCache Msg: 3rd MW

 

Https://docs.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline

Related Article

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.