本篇文章為大家展示了怎么在ASP.NET Core中對(duì)中間件進(jìn)行激活,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、南沙ssl等。為近千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的南沙網(wǎng)站制作公司IMiddlewareFactory/IMiddleware是中間件激活的擴(kuò)展點(diǎn)。
UseMiddleware擴(kuò)展方法檢查中間件的已注冊(cè)類型是否實(shí)現(xiàn)IMiddleware。如果是,則使用在容器中注冊(cè)的IMiddlewareFactory實(shí)例來解析IMiddleware實(shí)現(xiàn),而不使用基于約定的中間件激活邏輯。中間件在應(yīng)用的服務(wù)容器中注冊(cè)為作用域或瞬態(tài)服務(wù)。
優(yōu)點(diǎn):
按客戶端請(qǐng)求(作用域服務(wù)的注入)激活
讓中間件強(qiáng)類型化
IMiddleware按客戶端請(qǐng)求(連接)激活,因此作用域服務(wù)可以注入到中間件的構(gòu)造函數(shù)中。
IMiddleware
IMiddleware定義應(yīng)用的請(qǐng)求管道的中間件。InvokeAsync(HttpContext, RequestDelegate)方法處理請(qǐng)求,并返回代表中間件執(zhí)行的Task。
使用約定激活的中間件:
public class ConventionalMiddleware { private readonly RequestDelegate _next; public ConventionalMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context, AppDbContext db) { var keyValue = context.Request.Query["key"]; if (!string.IsNullOrWhiteSpace(keyValue)) { db.Add(new Request() { DT = DateTime.UtcNow, MiddlewareActivation = "ConventionalMiddleware", Value = keyValue }); await db.SaveChangesAsync(); } await _next(context); } }
使用MiddlewareFactory激活的中間件:
public class FactoryActivatedMiddleware : IMiddleware { private readonly AppDbContext _db; public FactoryActivatedMiddleware(AppDbContext db) { _db = db; } public async Task InvokeAsync(HttpContext context, RequestDelegate next) { var keyValue = context.Request.Query["key"]; if (!string.IsNullOrWhiteSpace(keyValue)) { _db.Add(new Request() { DT = DateTime.UtcNow, MiddlewareActivation = "FactoryActivatedMiddleware", Value = keyValue }); await _db.SaveChangesAsync(); } await next(context); } }
程序會(huì)為中間件創(chuàng)建擴(kuò)展:
public static class MiddlewareExtensions { public static IApplicationBuilder UseConventionalMiddleware( this IApplicationBuilder builder) { return builder.UseMiddleware(); } public static IApplicationBuilder UseFactoryActivatedMiddleware( this IApplicationBuilder builder) { return builder.UseMiddleware (); } }
無法通過UseMiddleware將對(duì)象傳遞給工廠激活的中間件:
public static IApplicationBuilder UseFactoryActivatedMiddleware( this IApplicationBuilder builder, bool option) { // Passing 'option' as an argument throws a NotSupportedException at runtime. return builder.UseMiddleware(option); }
將工廠激活的中間件添加到Startup.ConfigureServices
的內(nèi)置容器中:
public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseInMemoryDatabase("InMemoryDb")); services.AddTransient (); services.AddRazorPages(); }
兩個(gè)中間件均在Startup.Configure
的請(qǐng)求處理管道中注冊(cè):
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); } app.UseConventionalMiddleware(); app.UseFactoryActivatedMiddleware(); app.UseStaticFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); }
IMiddlewareFactory
IMiddlewareFactory提供中間件的創(chuàng)建方法。中間件工廠實(shí)現(xiàn)在容器中注冊(cè)為作用域服務(wù)。
可在Microsoft.AspNetCore.Http包中找到默認(rèn)的IMiddlewareFactory實(shí)現(xiàn)(即MiddlewareFactory)。
上述內(nèi)容就是怎么在ASP.NET Core中對(duì)中間件進(jìn)行激活,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。