您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在ASP.NET Core中對中間件進行激活,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
IMiddlewareFactory/IMiddleware是中間件激活的擴展點。
UseMiddleware擴展方法檢查中間件的已注冊類型是否實現IMiddleware。如果是,則使用在容器中注冊的IMiddlewareFactory實例來解析IMiddleware實現,而不使用基于約定的中間件激活邏輯。中間件在應用的服務容器中注冊為作用域或瞬態服務。
優點:
按客戶端請求(作用域服務的注入)激活
讓中間件強類型化
IMiddleware按客戶端請求(連接)激活,因此作用域服務可以注入到中間件的構造函數中。
IMiddleware
IMiddleware定義應用的請求管道的中間件。InvokeAsync(HttpContext, RequestDelegate)方法處理請求,并返回代表中間件執行的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); } }
程序會為中間件創建擴展:
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
的內置容器中:
public void ConfigureServices(IServiceCollection services) { services.AddDbContext<AppDbContext>(options => options.UseInMemoryDatabase("InMemoryDb")); services.AddTransient<FactoryActivatedMiddleware>(); services.AddRazorPages(); }
兩個中間件均在Startup.Configure
的請求處理管道中注冊:
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提供中間件的創建方法。中間件工廠實現在容器中注冊為作用域服務。
可在Microsoft.AspNetCore.Http包中找到默認的IMiddlewareFactory實現(即MiddlewareFactory)。
上述內容就是怎么在ASP.NET Core中對中間件進行激活,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。