您好,登錄后才能下訂單哦!
小編給大家分享一下如何在ASP.NET Core 2.0向中間件傳入初始參數,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
在一個空項目中,創建一個POCO(Plain Old CLR Object)來保存中間件所需的參數:
public class GreetingOptions { public string GreetAt { get; set; } public string GreetTo { get; set; } }
添加一個中間件:
public class GreetingMiddleware { private readonly RequestDelegate _next; private readonly GreetingOptions _options; public GreetingMiddleware(RequestDelegate next, GreetingOptions options) { _next = next; _options = options; } public async Task Invoke(HttpContext context) { var message = $"Good {_options.GreetAt} {_options.GreetTo}"; await context.Response.WriteAsync(message); } }
答案1:實例類型
添加一個擴展方法來配置中間件:
public static IApplicationBuilder UseGreetingMiddleware(this IApplicationBuilder app, GreetingOptions options) { return app.UseMiddleware<GreetingMiddleware>(options); }
使用中間件:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseGreetingMiddleware(new GreetingOptions { GreetAt = "Morning", GreetTo = "Tahir" }); }
答案2:函數類型
添加一個擴展方法來配置中間件:
public static IApplicationBuilder UseGreetingMiddlewareAction(this IApplicationBuilder app, Action<GreetingOptions> optionsAction) { var options = new GreetingOptions(); optionsAction(options); return app.UseMiddleware<GreetingMiddleware>(options); }
使用中間件:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseGreetingMiddlewareAction(options => { options.GreetAt = "Morning"; options.GreetTo = "Tahir"; }); }
上述兩種方法結果一致。
運行,此時頁面顯示:
討論
之前我們曾討論過,在單獨的類中定義中間件并使用擴展方法將其添加到請求管道中是最佳實踐。我們也可能需要向中間件傳入參數,通過對ASP.NET Core源代碼以及其他在線示例的學習,我總結出來上面兩種模式。
上述的兩種解決方法都非常直觀。我們將參數封裝到一個POCO類中,然后創建一個擴展方法來接受下面的參數:
1. POCO實例
2. 需要調用的函數(在函數內設置POCO)
注:POCO實例通過構造函數傳入中間件。UseMiddleware()方法接收可變參數params object[],并將這些參數傳入中間件構造函數。
配置服務
這些模式也能用于向服務容器中添加服務實例。為了便于說明,我們先添加一個服務:
public interface IMessageService { string FormatMessage(string message); } public class MessageService : IMessageService { private readonly GreetingOptions _options; public MessageService(GreetingOptions options) { _options = options; } public string FormatMessage(string message) { return $"Good {_options.GreetAt} {_options.GreetTo} - {message}"; } }
添加如下任一個擴展方法來配置服務:
public static IServiceCollection AddMessageService(this IServiceCollection services, GreetingOptions options) { return services.AddScoped<IMessageService>(factory => new MessageService(options)); } public static IServiceCollection AddMessageServiceAction(this IServiceCollection services, Action<GreetingOptions> optionsAction) { var options = new GreetingOptions(); optionsAction(options); return services.AddScoped<IMessageService>(factory => new MessageService(options)); }
在Configure()中使用此服務:
public void ConfigureServices(IServiceCollection services) { services.AddMessageService(new GreetingOptions { GreetAt = "Morning", GreetTo = "Tahir" }); services.AddMessageServiceAction(options => { options.GreetAt = "Morning"; options.GreetTo = "Tahir"; }); }
因為ConfigureServices()先于Configure()執行,因此我們可以直接在Configure()注入此服務:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMessageService msg) { app.Run(async (context) => { await context.Response.WriteAsync(msg.FormatMessage("by sanshi")); }); }
運行,此時頁面顯示:
看完了這篇文章,相信你對“如何在ASP.NET Core 2.0向中間件傳入初始參數”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。