.NET Core开发日志——Middleware

熟悉ASP.NET架构的开发者必定对于HTTP Modules与HTTP Handlers不陌生。二者的做用主要是对网络请求执行特定的处理工做。而在.NET Core中,它们都被Middleware(中件间)取代了。html

以前的Http Modules和HTTP Handlers是以下图般处理请求的:web

如今变成了这样:网络

一言归纳之,Middleware完成了HTTP Modules与HTTP Handlers的原有工做,但又不是简单的化二为一的减法做用。架构

Middleware减去的实际上是与原来ASP.NET中重要的基础——应用程序生命周期事件(application life cycle event)的绑定。app

HTTP Modules在初始化时就须要针对HttpApplication的事件做绑定处理,这样当HttpApplication的各项事件被触发时,已绑定的相应处理程序才会按照预期的那样被执行。async

public class HelloWorldModule : IHttpModule
{
    public HelloWorldModule()
    {
    }

    public String ModuleName
    {
        get { return "HelloWorldModule"; }
    }

    // In the Init function, register for HttpApplication 
    // events by adding your handlers.
    public void Init(HttpApplication application)
    {
        application.BeginRequest += 
            (new EventHandler(this.Application_BeginRequest));
        application.EndRequest += 
            (new EventHandler(this.Application_EndRequest));
    }

    private void Application_BeginRequest(Object source, 
         EventArgs e)
    {
    // Create HttpApplication and HttpContext objects to access
    // request and response properties.
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        context.Response.Write("<h1><font color=red>
            HelloWorldModule: Beginning of Request
            </font></h1><hr>");
    }

    private void Application_EndRequest(Object source, EventArgs e)
    {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        context.Response.Write("<hr><h1><font color=red>
            HelloWorldModule: End of Request</font></h1>");
    }

    public void Dispose()
    {
    }
}

而后你还须要在web.config配置文件注册这个HTTP Module。ui

<configuration>
    <system.web>
        <httpModules>
           <add name="HelloWorldModule" type="HelloWorldModule"/>
        </httpModules>
    </system.web>
</configuration>

若是是用Middleware的话,事情就变得很简单了。抛弃IHttpModule接口及HttpModule实现类,不用再关心HttpApplication的任何事件,还有烦人的web.config配置。直接在代码中以最简洁的方式完成工做。this

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.Use(async(context, next) =>{
        await context.Response.WriteAsync("Beginning of Request\n");
        await next.Invoke();
        await context.Response.WriteAsync("End of Request\n");
    });
    app.Run(async (context) =>
    {
        await context.Response.WriteAsync("Hello World!\n");
    });
}

类似的,对于HTTP Handlers,虽然不用取消对HttpApplication事件的依赖,但以二者的代码实现方式做比较,Middleware亳无疑问胜出一筹。3d

public class HelloWorldHandler : IHttpHandler
{
    public HelloWorldHandler()
    {
    }
    public void ProcessRequest(HttpContext context)
    {
        HttpRequest Request = context.Request;
        HttpResponse Response = context.Response;
        // This handler is called whenever a file ending 
        // in .sample is requested. A file with that extension
        // does not need to exist.
        Response.Write("<html>");
        Response.Write("<body>");
        Response.Write("<h1>Hello from a synchronous custom HTTP handler.</h1>");
        Response.Write("</body>");
        Response.Write("</html>");
    }
    public bool IsReusable
    {
        // To enable pooling, return true here.
        // This keeps the handler in memory.
        get { return false; }
    }
}

仍须要在web.config文件中注册HTTP handler。code

<configuration>
    <system.web>
        <httpHandlers>
            <add verb="*" path="*.sample" 
                  type="HelloWorldHandler"/>
        </httpHandlers>
    </system.web>
</configuration>

换做Middleware的写法:

private static void HandleSample(IApplicationBuilder app)
{
    app.Run(async context =>
    {
        await context.Response.WriteAsync("Hello Sample");
    });
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.MapWhen(context => context.Request.Path.Value.EndsWith("sample"), HandleSample);
}

总结下使用Middleware的优势:

  • 没有对HttpApplication的依赖
  • 没有对IHttpModule与IHttpHandler接口的依赖
  • 无需在web.config文件中添加各类配置
  • 代码简洁

最后须要补充Middleware与HTTP Modules的一点差别。各Middleware中处理请求与响应的顺序是恰好相反的,越早处理请求的Middleware越晚处理响应。而HTTP Modules中处理请求与响应的顺序则保持一致,由于每一个HTTP Module请求与响应事件的绑定都是在同一阶段完成的。