這篇文章主要介紹了ASP.NET Core 中基于工廠的中間件激活的實現(xiàn)方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
IMiddlewareFactory/IMiddleware是中間件激活的擴展點。
UseMiddleware擴展方法檢查中間件的已注冊類型是否實現(xiàn)IMiddleware。如果是,則使用在容器中注冊的IMiddlewareFactory實例來解析IMiddleware實現(xiàn),而不使用基于約定的中間件激活邏輯。中間件在應(yīng)用的服務(wù)容器中注冊為作用域或瞬態(tài)服務(wù)。
優(yōu)點:
按客戶端請求(作用域服務(wù)的注入)激活
讓中間件強類型化
IMiddleware按客戶端請求(連接)激活,因此作用域服務(wù)可以注入到中間件的構(gòu)造函數(shù)中。
IMiddleware
IMiddleware定義應(yīng)用的請求管道的中間件。InvokeAsync(HttpContext, RequestDelegate)方法處理請求,并返回代表中間件執(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);
}
}
程序會為中間件創(chuàng)建擴展:
public static class MiddlewareExtensions
{
public static IApplicationBuilder UseConventionalMiddleware(
this IApplicationBuilder builder)
{
return builder.UseMiddleware<ConventionalMiddleware>();
}
public static IApplicationBuilder UseFactoryActivatedMiddleware(
this IApplicationBuilder builder)
{
return builder.UseMiddleware<FactoryActivatedMiddleware>();
}
}
無法通過UseMiddleware將對象傳遞給工廠激活的中間件:
public static IApplicationBuilder UseFactoryActivatedMiddleware(
this IApplicationBuilder builder, bool option)
{
// Passing 'option' as an argument throws a NotSupportedException at runtime.
return builder.UseMiddleware<FactoryActivatedMiddleware>(option);
}
將工廠激活的中間件添加到Startup.ConfigureServices的內(nèi)置容器中:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options =>
options.UseInMemoryDatabase("InMemoryDb"));
services.AddTransient<FactoryActivatedMiddleware>();
services.AddRazorPages();
}
將工廠激活的中間件添加到Startup.ConfigureServices的內(nèi)置容器中:
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)建方法。中間件工廠實現(xiàn)在容器中注冊為作用域服務(wù)。
可在Microsoft.AspNetCore.Http包中找到默認(rèn)的IMiddlewareFactory實現(xiàn)(即MiddlewareFactory)。
到此這篇關(guān)于ASP.NET Core 中基于工廠的中間件激活詳解的文章就介紹到這了,更多相關(guān)ASP.NET Core中間件激活內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
來源:腳本之家
鏈接:https://www.jb51.net/article/199708.htm
申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!