什么是中间件
中间件是在管道中处理Request请求与Responses响应的一种组件,每种组件可以选择是否让Request进入到下一个组件去处理。
译得不好,大家可以自己看原文Middleware
更详细的还可以参照园中大神的作品;
有汤姆大叔的解读ASP.NET 5 & MVC6系列(6):Middleware详解
artech大神的 ASP.NET Core真实管道详解[1]:中间件是个什么东西?
怎么创建一个Middleware请参考英文文档Middleware 或者
LineZero的 ASP.NET Core 开发-中间件(Middleware)
要正确使用Middleware来构建自己的应该程序,需要理解Run,Use,Map,MapThen这四个方法是如何使用的, 下面Ricman将自己的理解与大家分享。
一、Run扩展方法
Run方法在说明上是这样的:在管道的尾端增加一个Middleware;它是执行的最后一个Middleware。即它执行完就不再执行下一个Middleware了。如下代码示例。
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole(Configuration.GetSection("Logging"));loggerFactory.AddDebug();
var loger = loggerFactory.CreateLogger("TestLogger"); //第一个Run 执行了app.Run(async context =>{loger.LogInformation("run 1 start"); await context.Response.WriteAsync("hello world!,run 1");loger.LogInformation("run 1 end");}); //第二个Run 没的执行app.Run(async context =>{loger.LogInformation("run 2 start"); await context.Response.WriteAsync("hello world!,run 2");loger.LogInformation("run 2 end");});}
输出的结果为:
只打印出了第一个Run中的内容。而程序也不会响应第二个Run方法中的内容。
二、Use扩展方法
Use方法,则是在管道中增加一个Middleware。如果调用了next.Invoke()方法,它会去执行下一个Middleware 。我们把上面的例子稍作修改:
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole(Configuration.GetSection("Logging"));loggerFactory.AddDebug();
var loger = loggerFactory.CreateLogger("TestLogger"); //use 方法 执行了app.Use (async (context,next) =>{loger.LogInformation("Use 1 start"); await context.Response.WriteAsync("hello world!,Use 1");loger.LogInformation("Use 1 end");}); //Run 方法没的执行app.Run(async context =>{loger.LogInformation("run 1 start"); await context.Response.WriteAsync("hello world!,run 1");loger.LogInformation("run 1 end");});}
输出结果是什么?
没有调用next.Invoke();尾端的Middleware即Run方法内没有执行。使用Use方法,而没有调用next.Invoke(),Use的效果与Run的效果是一致的。为了验证Use 的效果,我们再修改代码。
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole(Configuration.GetSection("Logging"));loggerFactory.AddDebug();
var loger = loggerFactory.CreateLogger("TestLogger"); //执行了app.Use (async (context,next) =>{loger.LogInformation("Use 1 start"); await context.Response.WriteAsync("hello world!,Use "); await next.Invoke();loger.LogInformation("Use 1 end");}); //没的执行app.Run(async context =>{loger.LogInformation("run 1 start"); await context.Response.WriteAsync(" hello world!,run ");loger.LogInformation("run 1 end");});}
此时,输入以下的结果
即Use与Run代码段都被执行了。需要注意的是,管道中可以增加多个middleware,他们是按顺序执行的,执行的顺序与在Configure方法中代码的顺序是一致的。
三、Map与MapThen
Map比较不同,它将Middleware添加到管道中,它是在管道中增加了分支。通过影射路径的方式,增加管道分支。我们保留上面例子,并增加代码。如下:
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole(Configuration.GetSection("Logging"));loggerFactory.AddDebug();
var loger = loggerFactory.CreateLogger("TestLogger"); //执行了app.Use (async (context,next) =>{loger.LogInformation("Use 1 start"); await context.Response.WriteAsync("hello world!,Use "); await next.Invoke();loger.LogInformation("Use 1 end");});app.Map("/mapTest", HandleMap); //没的执行app.Run(async context =>{loger.LogInformation("run 1 start"); await context.Response.WriteAsync(" hello world!,run ");loger.LogInformation("run 1 end");});} private static void HandleMap(IApplicationBuilder app){app.Run(async context =>{ await context.Response.WriteAsync("Hello ,that is Handle Map ");});}
运行起来,我们在浏览器中输入” http://localhost:12716/mapTest” 得到的结果如下:
mapTest分支被执行了。
MapThen就更有意思,从字面上感觉有点类似查询的意思。对了。它就是处理符合条件的Request去执行给定的方法。我们修改代码
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole(Configuration.GetSection("Logging"));loggerFactory.AddDebug();
var loger = loggerFactory.CreateLogger("TestLogger"); //执行了app.Use (async (context,next) =>{loger.LogInformation("Use 1 start"); await context.Response.WriteAsync("hello world!,Use "); await next.Invoke();loger.LogInformation("Use 1 end");});app.MapWhen(context =>{ return context.Request.Query.ContainsKey("q"); }, HandleQuery); //没的执行app.Run(async context =>{loger.LogInformation("run 1 start"); await context.Response.WriteAsync(" hello world!,run ");loger.LogInformation("run 1 end");});} private static void HandleQuery(IApplicationBuilder app){app.Run(async context =>{ await context.Response.WriteAsync(" Hello ,this is Handle Query ");});}
我们要处理的是:如果有URL中的参数包含了q字母的话,就去执行HandleQuery方法。看一下结果
可以看出来,MapWhen可以处理很多的东西,比如我们要处理Request表头中某特定的内容,可以使用MapWhen来处理。
相关文章:
微软.NET 正式劈腿成功,横跨所有平台
.NET Core 1.0 CentOS7 尝试
解读发布:.NET Core RC2 and .NET Core SDK Preview 1
[.NET Core].NET Core R2安装及示例教程
ASP.NET Core 开发-中间件(Middleware)
结合Jexus + Kestrel 部署 asp.net core 生产环境
通过Jexus 部署 dotnetcore版本MusicStore 示例程序
ASP.NET Core 中文文档 第一章 入门
用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序
用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API
用 Visual Studio 发布一个 Azure 云 Web 应用程序
ASP.NET Core MVC 与 Visual Studio 入门
第二章指南(4.2)添加 Controller
DotNet Core 介绍
asp.net core 中间件详解及项目实战
教你实践ASP.NET Core Authorization(免看文档教程)
asp.net core 使用 Redis 和 Protobuf 进行 Session 缓存
asp.net core 中间件详解及项目实战
第二章 指南(4.3)添加 View
dotnet core开发体验之开始MVC
dotnet core 开发体验之Routing
聊聊ASP.NET Core默认提供的这个跨平台的服务器——KestrelServer
简析.NET Core 以及与 .NET Framework的关系
.NET Core 使用Dapper 操作MySQL
使用 CommandLineApplication 类创建专业的控制台程序
简析 .NET Core 构成体系
.NET Core也可以使用MongoDB了
.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布
.NET Core:面向未来的开源跨平台开发技术
微软说它深爱着Linux,现在它用行动证明了
移植.NET Core计划,整合各平台变得更简单了!
ASP.NET Core 介绍
通过几个Hello World感受.NET Core全新的开发体验
ASP.NET Core 运行原理剖析1:初始化WebApp模版并运行
.NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门
Asp.Net Core 发布和部署( MacOS + Linux + Nginx )
Asp.Net Core 发布和部署(Linux + Jexus )
学习ASP.NET Core,你必须了解无处不在的“依赖注入”
.NET Core应用类型(Portable apps & Self-contained apps)
.NET Core 1.0发布:微软开源跨平台大布局序幕
ASP.NET Core 运行原理剖析2:Startup 和 Middleware(中间件)
在Windows Server 2012 R2 Standard 部署 ASP.NET Core程序
ASP.NET Core 开发-Entity Framework (EF) Core 1.0 Database First
拥抱.NET Core,跨平台的轻量级RPC:Rabbit.Rpc
使用 dotnet watch 开发 ASP.NET Core 应用程序
ASP.NET Core 发布至Linux生产环境 Ubuntu 系统
ASP.NET Core Docker部署
ASP.NET Core "完整发布,自带运行时" 到jexus
全球首发免费的MySql for Entity Framework Core
原文地址:http://www.cnblogs.com/xiaoshou/p/5669122.html
.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注