ASP.NET Core實(shí)現(xiàn)中間件的幾種方式
前言
ASP.NET Core 中 HTTP 管道使用中間件組合處理的方式,
換句人話來說,
對(duì)于寫代碼的人而言,一切皆中間件.
業(yè)務(wù)邏輯/數(shù)據(jù)訪問/等等一切都需要以中間件的方式來呈現(xiàn).
那么我們必須學(xué)會(huì)如何實(shí)現(xiàn)自定義中間件 這里劃重點(diǎn),必考
這里我們介紹下中間件的幾種實(shí)現(xiàn)方式...
匿名函數(shù)
通常新建一個(gè)空的 ASP.NET Core Web Application,項(xiàng)目名字無所謂啦
在啟動(dòng)類里可以看到這么一句:
// Startup.cs // ... app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); // ...
這就是一個(gè)匿名函數(shù)實(shí)現(xiàn)的中間件,雖然內(nèi)容比較少.
可以看到通過匿名函數(shù)實(shí)現(xiàn)的中間件是內(nèi)嵌在啟動(dòng)類文件中的,因此通常也叫做內(nèi)聯(lián)中間件
接下來,我們通過匿名函數(shù)來實(shí)現(xiàn)內(nèi)聯(lián)中間件,以便加深理解.
然后修改啟動(dòng)類代碼如下:
// Startup.cs using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using System; namespace WebApplication1 { public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 使用匿名函數(shù)實(shí)現(xiàn)一個(gè)內(nèi)聯(lián)中間件 app.Use(async (context, next) => { throw new NotImplementedException("一個(gè)使用匿名函數(shù),但未實(shí)現(xiàn)具體內(nèi)容的內(nèi)聯(lián)中間件"); }); app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } } }
這里我們?cè)?nbsp;app.Run
之前使用 app.Use
添加一個(gè)匿名函數(shù)實(shí)現(xiàn)的內(nèi)聯(lián)中間件,按照中間件的注冊(cè)順序,當(dāng)發(fā)起請(qǐng)求時(shí),會(huì)拋出一個(gè)異常 NotImplementedException("一個(gè)使用匿名函數(shù),但未實(shí)現(xiàn)具體內(nèi)容的內(nèi)聯(lián)中間件")
我們 F5 啟動(dòng)下,看看頁面
嗯,符合預(yù)期.
我們?cè)賮碚{(diào)整下啟動(dòng)類,代碼如下:
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace WebApplication1 { public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 使用匿名函數(shù)實(shí)現(xiàn)一個(gè)內(nèi)聯(lián)中間件 app.Use(async (context, next) => { // 這里不對(duì) request 做任何處理,直接調(diào)用下一個(gè)中間件 await next.Invoke(); }); app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } } }
這里我們?cè)?nbsp;app.Run
之前使用 app.Use
添加一個(gè)匿名函數(shù)實(shí)現(xiàn)的內(nèi)聯(lián)中間件,該中間件沒有對(duì) request 做任何處理,只是一個(gè)空的空間件,按照中間件的注冊(cè)順序,當(dāng)發(fā)起請(qǐng)求時(shí),頁面應(yīng)該顯示 Hello World!
.
我們 F5 啟動(dòng),看看效果
嗯,符合預(yù)期.
個(gè)人覺得:匿名函數(shù)不是很直觀,但是用內(nèi)聯(lián)的方式可以快速開始一些開發(fā),不用新建一個(gè)中間件類,不用專門想個(gè)不一樣的名字,小場(chǎng)景下是非常方便實(shí)用的
實(shí)現(xiàn)接口
通過實(shí)現(xiàn)接口 IMiddleware
編寫自定義中間件,這是一種強(qiáng)類型的方式,我們需要必須強(qiáng)制按照接口的定義來實(shí)現(xiàn).
IMiddleware
接口 IMiddleware 定義如下:
using System.Threading.Tasks; namespace Microsoft.AspNetCore.Http { public interface IMiddleware { Task InvokeAsync(HttpContext context, RequestDelegate next); } }
可以看到接口 IMiddleware 的命名空間是 Microsoft.AspNetCore.Http
,需要實(shí)現(xiàn)的方法是InvokeAsync()
,看起來不算太復(fù)雜, 嗯,看起來不算太復(fù)雜
嗯,重新開始,我們新建一個(gè)空的 ASP.NET Core Web Application
然后我們通過實(shí)現(xiàn)接口的方式來自定義一個(gè)中間件,代碼如下:
// 新建類 MyMiddleware.cs using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace WebApplication1 { public class MyMiddleware : IMiddleware { public Task InvokeAsync(HttpContext context, RequestDelegate next) { throw new NotImplementedException(); } } }
按照上面實(shí)現(xiàn)的中間件 MyMiddleware
,在執(zhí)行時(shí)應(yīng)該會(huì)拋出 NotImplementedException
.
使用接口實(shí)現(xiàn)的中間件需要在先在服務(wù)容器中注冊(cè)
// Startup.cs using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace WebApplication1 { public class Startup { public void ConfigureServices(IServiceCollection services) { // 在服務(wù)容器中注冊(cè)自定義中間件 services.AddSingleton<MyMiddleware>(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 使用 UseMiddleware() 把自定義中間件添加到管道中 app.UseMiddleware<MyMiddleware>(); app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } } }
然后 F5 啟動(dòng),頁面上可以看到如下結(jié)果:
符合我們上面的預(yù)期,拋出了一個(gè) NotImplementedException
.
然后我們改造下 MyMiddleware
中間件
// MyMiddleware.cs using Microsoft.AspNetCore.Http; using System.Threading.Tasks; namespace WebApplication1 { public class MyMiddleware : IMiddleware { public async Task InvokeAsync(HttpContext context, RequestDelegate next) { // 這里不對(duì) request 做任何處理,直接調(diào)用下一個(gè)中間件 await next(context); } } }
這里相當(dāng)于我們實(shí)現(xiàn)了一個(gè)叫做 MyMiddleware
的中間件,但是并沒有對(duì)請(qǐng)求進(jìn)行任何處理,頁面上應(yīng)該正常顯示 Hello World!
字符串.
然后我們 F5 啟動(dòng)看看
嗯...符合預(yù)期.
個(gè)人覺得:這種方式最符合面向?qū)ο蟮奶匦?也符合面向接口的原則,少一些難以理解的魔法,反而有助于理解.
約定方式
編程世界有這么一句話,叫"約定大于配置".
那么編寫中間件的約定是什么呢?
重新開始,新建一個(gè)空的 ASP.NET Core Web Application
然后新建一個(gè)類,類名叫做 MyMiddleware
好了,代碼如下:
// MyMiddleware.cs using Microsoft.AspNetCore.Http; using System; using System.Threading.Tasks; namespace WebApplication1 { public class MyMiddleware { // 1. 需要實(shí)現(xiàn)一個(gè)構(gòu)造函數(shù),參數(shù)為 RequestDelegate public MyMiddleware(RequestDelegate next) { } // 2. 需要實(shí)現(xiàn)一個(gè)叫做 InvokeAsync 方法 public async Task InvokeAsync(HttpContext context) { throw new NotImplementedException("這是一個(gè)按照約定方式編寫的中間件,但未實(shí)現(xiàn)具體內(nèi)容"); } } }
約定的內(nèi)容,就是滿足2個(gè)需要...不滿足需要?jiǎng)t異常.
然后我們把這個(gè)中間件,注冊(cè)到管道中,以便使用
// Startup.cs using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace WebApplication1 { public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 注冊(cè)自定義中間件 // 注冊(cè)順序=1 app.UseMiddleware<MyMiddleware>(); app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } } }
然后 F5 啟動(dòng),來看看效果
嗯,符合預(yù)期.
然后我們來調(diào)整下中間件,讓請(qǐng)求能正常響應(yīng)輸出 Hello World!
using Microsoft.AspNetCore.Http; using System; using System.Threading.Tasks; namespace WebApplication1 { public class MyMiddleware { private readonly RequestDelegate _next; // 需要實(shí)現(xiàn)一個(gè)構(gòu)造函數(shù),參數(shù)為 RequestDelegate public MyMiddleware(RequestDelegate next) { _next = next; } // 需要實(shí)現(xiàn)一個(gè)叫做 InvokeAsync 方法 public async Task InvokeAsync(HttpContext context) { // 不處理任何 request, 直接調(diào)用下一個(gè)中間件 await _next.Invoke(context); } } }
然后 F5 啟動(dòng),看看效果
嗯,符合預(yù)期.
個(gè)人覺得:只能說一句,約定方式是目前用的最多的方式...
End
寫在最后
Tips: 有些內(nèi)容可能看起來還是不太容易理解,至少當(dāng)下你是很難理解的,但是套路就在哪里,好比1+1=2,你知道1+1為什么=2么?但你一定會(huì)算會(huì)用1+1=2...
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
.NET讀寫Excel工具Spire.Xls使用 Excel單元格控制(3)
這篇文章主要為大家詳細(xì)介紹了.NET讀寫Excel工具Spire.Xls使用,Excel單元格控制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11xpath的數(shù)據(jù)和節(jié)點(diǎn)類型以及XPath中節(jié)點(diǎn)匹配的基本方法
xpath的數(shù)據(jù)和節(jié)點(diǎn)類型以及XPath中節(jié)點(diǎn)匹配的基本方法,學(xué)習(xí)xpath的朋友可以參考下。2010-09-09CheckBoxList兩列并排編譯為表格顯示具體實(shí)現(xiàn)
CheckBoxList兩列并排的顯示效果相比大家都有見到過吧,下面是具體的實(shí)現(xiàn)代碼,感興趣的朋友可以參考下哈2013-05-05HttpWebRequest的常見錯(cuò)誤使用TcpClient可避免
有時(shí)使用HttpWebRequest對(duì)象會(huì)出現(xiàn)錯(cuò)誤有三種服務(wù)器提交了協(xié)議沖突/基礎(chǔ)連接已經(jīng)關(guān)閉:連接被意外關(guān)閉/無法發(fā)送具有此謂詞類型的內(nèi)容正文,感興趣的朋友可以參考下本文2013-02-02解決Asp.net Mvc返回JsonResult中DateTime類型數(shù)據(jù)格式問題的方法
這篇文章主要介紹了解決Asp.net Mvc返回JsonResult中DateTime類型數(shù)據(jù)格式問題的方法,需要的朋友可以參考下2016-06-06ASP.NET Core中調(diào)整HTTP請(qǐng)求大小的幾種方法詳解
這篇文章主要給大家介紹了關(guān)于在ASP.NET Core中如何調(diào)整HTTP請(qǐng)求大小的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12