Owin Middleware如何在IIS集成管道中执行_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > Owin Middleware如何在IIS集成管道中执行

Owin Middleware如何在IIS集成管道中执行

 2017/8/13 11:31:23  麻将我会  程序员俱乐部  我要评论(0)
  • 摘要:OwinMiddlewareComponents(OMCs)通过安装Install-PackageMicrosoft.Owin.Host.SystemWeb可以让OMCs在IIS集成管道下工作在IIS集成管道里,这个requestpipeline包含HttpModules关联到一组预定义的管道事件,例如BeginRequest,AuthenticateRequest,AuthorizeRequest,等如果我们将OMC和HttpModule进行比较,OMC也和HttpModule一样
  • 标签:iiS Middleware 执行

Owin Middleware Components(OMCs)

通过安装class="lang-console" data-src="https://github.com/aspnet/Docs/blob/live/aspnet/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline/samples/sample1.cmd" data-bi-name="code-block">Install-Package Microsoft.Owin.Host.SystemWeb

可以让OMCs在IIS集成管道下工作

在IIS集成管道里,这个request pipeline 包含HttpModules关联到一组预定义的管道事件,例如

BeginRequest, AuthenticateRequest, AuthorizeRequest,等

如果我们将OMC和HttpModule进行比较,OMC也和HttpModule一样,必须要被注册到一个恰当的预定义的管道事件里,比如下面的这个Httpmodule,

当一个请求来到AuthenticateRequest阶段时,MyModule会被调用

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.
    }
}

 为了使OMC取参与和HttpModule相同的基于事件的执行顺序,Katana运行时代码扫描Startup配置,并且把每个OMC关联到某个集成管道事件里,

比如下面的配置:

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);
        }
    }
}

 输出如下:

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

 可以看到Katana运行时默认映射每个OMC到IIS管道事件PreRequestHandlerExecute

你可以根据需要调整这个OMC和管道事件的这种默认关系,具体使用一个扩展方法IAppBuilder UseStageMarker(),

像下面这样:

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);
}

 输出如下:

Current IIS event: AuthenticateRequest Msg: Middleware 1
Current IIS event: AuthenticateRequest Msg: 2nd MW
Current 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

发表评论
用户名: 匿名