ASP.NET Core 1.0借鉴了Katana项目的管道设计(Pipeline)。日志记录、用户认证、MVC等模块都以中间件(Middleware)的方式注册在管道中。显而易见这样的设计非常松耦合并且非常灵活,你可以自己定义任意功能的Middleware注册在管道中。这一设计非常适用于“请求-响应”这样的场景——消息从管道头流入最后反向流出。

在本文中暂且为这种模式起名叫做“管道-中间件(Pipeline-Middleware)”模式吧。

本文将描述”管道-中间件模式”的“契约式”设计和“函数式”设计两种方案。

一、什么是管道-中间件模式?

在此模式中抽象了一个类似管道的概念,所有的组件均以中间件的方式注册在此管道中,当请求进入管道后:中间件依次对请求作出处理,然后从最后一个中间件开始处理响应内容,最终反向流出管道。

二、契约式设计

契约式设计是从面向对象的角度来思考问题,根据管道-中间件的理解,中间件(Middleware)有两个职责:

    public interface IMiddleware
{
Request ProcessRequest(Request request);
Response ProcessResponse(Response response);
}

管道(Pipeline)抽象应该能够注册中间件(Middleware):

    public interface IApplicationBuilder
{ void Use(IMiddleware middleware); void UseArrange(List<IMiddleware> middlewares); Context Run(Context context);
}

实现IApplicationBuilder:

    public class ApplicationBuilder : IApplicationBuilder
{
public IWindsorContainer Container { get; private set; }
private readonly List<IMiddleware> _middlewares; public ApplicationBuilder(IWindsorContainer container)
{
Contract.Requires(container!=null,"container!=null"); _middlewares=new List<IMiddleware>();
Container = container;
} public void Use(IMiddleware middleware)
{
Contract.Requires(middleware != null, "middleware!=null"); _middlewares.Add(middleware);
} public void UseArrange(List<IMiddleware> middlewares)
{
Contract.Requires(middlewares != null, "middlewares!=null"); _middlewares.AddRange(middlewares);
} public Context Run(Context context)
{
Contract.Requires(context!=null,"context!=null"); var request=context.Request;
var response=context.Response; foreach (var middleware in _middlewares)
{
request = middleware.ProcessRequest(request);
} _middlewares.Reverse(); foreach (var middleware in _middlewares)
{
response = middleware.ProcessResponse(response);
} return new Context(request,response);
}
}

Run()方法将依次枚举Middleware并对消息的请求和响应进行处理,最后返回最终处理过的消息。

接下来需要实现一个Middleware:

   public class DefaultMiddleware:IMiddleware
{
public Request ProcessRequest(Request request)
{
request.Process("default request", "processed by defaultMiddleware");
return request;
} public Response ProcessResponse(Response response)
{
response.Process("default response", "processed by defaultMiddleware");
return response;
}
}

为了将Middleware注册进管道,我们还可以写一个扩展方法增加代码的可读性:

        public static void UseDefaultMiddleware(this IApplicationBuilder applicationBuilder)
{
applicationBuilder.Use<DefaultMiddleware>();
} public static void Use<TMiddleware>(this IApplicationBuilder applicationBuilder)
where TMiddleware:IMiddleware
{
var middleware = applicationBuilder.Container.Resolve<TMiddleware>(); applicationBuilder.Use(middleware);
}

写个测试看看吧:

写第二个Middleware:

    public class GreetingMiddleware:IMiddleware
{
public Request ProcessRequest(Request request)
{
request.Process("hello, request","processed by greetingMiddleware"); return request;
} public Response ProcessResponse(Response response)
{
response.Process("hello, request", "processed by greetingMiddleware"); return response;
}
}

编写测试:

三、函数式设计方案

此方案也是Owin和ASP.NET Core采用的方案,如果站在面向对象的角度,第一个方案是非常清晰的,管道最终通过枚举所有Middleware来依次处理请求。

站在函数式的角度来看,Middleware可以用Func<Context, Context>来表示,再来看看这张图:

一个Middleware的逻辑可以用Func<Func<Context, Context>, Func<Context, Context>>来表示,整个Middleware的逻辑可以用下面的代码描述:

        public Func<Func<Context, Context>, Func<Context, Context>> Process()
{
Func<Func<Context, Context>, Func<Context, Context>> middleware = next =>
{
Func<Context, Context> process = context =>
{
/*process request*/ next(context); /*process response*/ return context;
}; return process;
}; return middleware;
}

这一过程是理解函数式方案的关键,所有Middleware可以聚合为一个Func<Context,Context>,为了易于阅读,我们可以定义一个委托:

public delegate Context RequestDelegate(Context context);

给定初始RequestDelegate,聚合所有Middleware:

        public IApplication Build()
{
RequestDelegate request = context => context; _middlewares.Reverse(); foreach (var middleware in _middlewares)
{
request = middleware(request);
} return new Application(request);
}

自定义一个函数式Middleware:

    public class DefaultMiddleware:IMiddleware
{
public Func<RequestDelegate, RequestDelegate> Request()
{
Func<RequestDelegate, RequestDelegate> request = next =>
{
return context =>
{
context.Request.Process("default request", "processed by defaultMiddleware"); next(context); context.Response.Process("default response", "processed by defaultMiddleware"); return context;
}; }; return request;
}
}

所有代码提供下载:https://git.oschina.net/richieyangs/Pipeline.Middleware.git

ASP.NET Core 1.0中的管道-中间件模式的更多相关文章

  1. Core 1.0中的管道-中间件模式

    ASP.NET Core 1.0中的管道-中间件模式 SP.NET Core 1.0借鉴了Katana项目的管道设计(Pipeline).日志记录.用户认证.MVC等模块都以中间件(Middlewar ...

  2. 探索ASP.Net Core 3.0系列二:聊聊ASP.Net Core 3.0 中的Startup.cs

    原文:探索ASP.Net Core 3.0系列二:聊聊ASP.Net Core 3.0 中的Startup.cs 前言:.NET Core 3.0 SDK包含比以前版本更多的现成模板. 在本文中,我将 ...

  3. ASP.NET Core 1.0 中的依赖项管理

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  4. 在ASP.NET Core 1.0中如何发送邮件

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:目前.NET Core 1.0中并没有提供SMTP相关的类库,那么要如何从ASP.NE ...

  5. ASP.NET Core 1.0 中使用 Swagger 生成文档

    github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1. ...

  6. 用ASP.NET Core 1.0中实现邮件发送功能

    准备将一些项目迁移到 asp.net core 先从封装类库入手,在遇到邮件发送类时发现在 asp.net core 1.0中并示提供SMTP相关类库,于是网上一搜发现了MailKit 好东西一定要试 ...

  7. 在ASP.NET Core 2.0中使用CookieAuthentication

    在ASP.NET Core中关于Security有两个容易混淆的概念一个是Authentication(认证),一个是Authorization(授权).而前者是确定用户是谁的过程,后者是围绕着他们允 ...

  8. 如何在ASP.NET Core 2.0中使用Razor页面

    如何在ASP.NET Core 2.0中使用Razor页面  DotNetCore2017-11-22 14:49 问题 如何在ASP.NET Core 2.0中使用Razor页面 解 创建一个空的项 ...

  9. ASP.NET Core 3.0中使用动态控制器路由

    原文:Dynamic controller routing in ASP.NET Core 3.0 作者:Filip W 译文:https://www.cnblogs.com/lwqlun/p/114 ...

随机推荐

  1. ***CodeIgniter集成微信支付(转)

    微信支付Native扫码支付模式二之CodeIgniter集成篇  http://www.cnblogs.com/24la/p/wxpay-native-qrcode-codeigniter.html ...

  2. sql server 锁

     锁模式 锁模式 说明 共享 (S) 用于不更改或不更新数据的读取操作,如 SELECT 语句. 更新 (U) 用于可更新的资源中. 防止当多个会话在读取.锁定以及随后可能进行的资源更新时发生常见形式 ...

  3. (转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)

    string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" + filePath + ";Exten ...

  4. 自己用的jquery经常用的工具command

    var Cmd = { Entity: { QueryString: {}, }, RootPath: function () { var pathName = window.location.pat ...

  5. Spring事务

    1.@Transactional 只能被应用到public方法上, 对于其它非public的方法,如果标记了@Transactional也不会报错,但方法没有事务功能.@Transactional 的 ...

  6. ubuntu 安装phpstorm

    1.清除 sudo apt-get purge openjdk* 2.添加源及更新源列表 sudo add-apt-repository ppa:webupd8team/java sudo apt-g ...

  7. Python2 新手 编码问题 吐血总结

    什么是编码 任何一种语言.文字.符号等等,计算都是将其以一种类似字典的形式存起来的,比如最早的计算机系统将英文文字转为数字存储(ASCII码),这种文字与数字(或其他)一一对应的关系我们称之为编码.由 ...

  8. Arduino下LCD1602综合探究(上)——1602的两种驱动方式,如何使LCD的控制编程变得更简单

    一.前言: LCD ( Liquid Crystal Display 的简称)液晶显示器,已经逐渐替代CRT成为主流的显示设备之一,因此也成为了单片机发烧友绕不过的话题之一:而LCD1602更是很多单 ...

  9. ASP.NET图形验证码的生成

    效果: 调用方法: int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列 );//产生验证码字符 pictureBox1.Image = QAPI.Ver ...

  10. Nodejs 创建web服务

    // 加载http服务包 var http = require("http"); // 创建服务 var server = http.createServer(requestLis ...