栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

.NET Core开发日志——Middleware

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.NET Core开发日志——Middleware

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

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

现在变成了这样:

一言概括之,Middleware完成了HTTP Modules与HTTP Handlers的原有工作,但又不是简单的化二为一的减法作用。

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

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

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("
            HelloWorldModule: Beginning of Request
            
");     }    private void Application_EndRequest(Object source, EventArgs e)    {         HttpApplication application = (HttpApplication)source;         HttpContext context = application.Context;         context.Response.Write("
            HelloWorldModule: End of Request");     }    public void Dispose()    {     } }

然后你还需要在web.config配置文件注册这个HTTP Module。


    
        
           
        
    

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

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

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

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("");
        Response.Write("");
        Response.Write("Hello from a synchronous custom HTTP handler.");
        Response.Write("");
        Response.Write("");
    }    public bool IsReusable
    {        // To enable pooling, return true here.
        // This keeps the handler in memory.
        get { return false; }
    }
}

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


    
        
            
        
    

换作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请求与响应事件的绑定都是在同一阶段完成的。

原文出处

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/231306.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号