問題
如何在ASP.NET Core 2.0向中間件傳入初始參數(shù)?
答案
在一個(gè)空項(xiàng)目中,創(chuàng)建一個(gè)POCO(Plain Old CLR Object)來保存中間件所需的參數(shù):
1
2
3
4
5
|
public class GreetingOptions { public string GreetAt { get ; set ; } public string GreetTo { get ; set ; } } |
添加一個(gè)中間件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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:實(shí)例類型
添加一個(gè)擴(kuò)展方法來配置中間件:
1
2
3
4
|
public static IApplicationBuilder UseGreetingMiddleware( this IApplicationBuilder app, GreetingOptions options) { return app.UseMiddleware<GreetingMiddleware>(options); } |
使用中間件:
1
2
3
4
5
6
7
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseGreetingMiddleware( new GreetingOptions { GreetAt = "Morning" , GreetTo = "Tahir" }); } |
答案2:函數(shù)類型
添加一個(gè)擴(kuò)展方法來配置中間件:
1
2
3
4
5
6
7
|
public static IApplicationBuilder UseGreetingMiddlewareAction( this IApplicationBuilder app, Action<GreetingOptions> optionsAction) { var options = new GreetingOptions(); optionsAction(options); return app.UseMiddleware<GreetingMiddleware>(options); } |
使用中間件:
1
2
3
4
5
6
7
8
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseGreetingMiddlewareAction(options => { options.GreetAt = "Morning" ; options.GreetTo = "Tahir" ; }); } |
上述兩種方法結(jié)果一致。
運(yùn)行,此時(shí)頁面顯示:
討論
之前我們曾討論過,在單獨(dú)的類中定義中間件并使用擴(kuò)展方法將其添加到請求管道中是最佳實(shí)踐。我們也可能需要向中間件傳入?yún)?shù),通過對ASP.NET Core源代碼以及其他在線示例的學(xué)習(xí),我總結(jié)出來上面兩種模式。
上述的兩種解決方法都非常直觀。我們將參數(shù)封裝到一個(gè)POCO類中,然后創(chuàng)建一個(gè)擴(kuò)展方法來接受下面的參數(shù):
1. POCO實(shí)例
2. 需要調(diào)用的函數(shù)(在函數(shù)內(nèi)設(shè)置POCO)
注:POCO實(shí)例通過構(gòu)造函數(shù)傳入中間件。UseMiddleware()方法接收可變參數(shù)params object[],并將這些參數(shù)傳入中間件構(gòu)造函數(shù)。
配置服務(wù)
這些模式也能用于向服務(wù)容器中添加服務(wù)實(shí)例。為了便于說明,我們先添加一個(gè)服務(wù):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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}" ; } } |
添加如下任一個(gè)擴(kuò)展方法來配置服務(wù):
1
2
3
4
5
6
7
8
9
|
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()中使用此服務(wù):
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public void ConfigureServices(IServiceCollection services) { services.AddMessageService( new GreetingOptions { GreetAt = "Morning" , GreetTo = "Tahir" }); services.AddMessageServiceAction(options => { options.GreetAt = "Morning" ; options.GreetTo = "Tahir" ; }); } |
因?yàn)镃onfigureServices()先于Configure()執(zhí)行,因此我們可以直接在Configure()注入此服務(wù):
1
2
3
4
5
6
7
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMessageService msg) { app.Run(async (context) => { await context.Response.WriteAsync(msg.FormatMessage( "by sanshi" )); }); } |
運(yùn)行,此時(shí)頁面顯示:
點(diǎn)擊下載源碼:MiddlewareWithParameters.rar
總結(jié)
以上所述是小編給大家介紹的ASP.NET Core 2.0 帶初始參數(shù)的中間件問題及解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/sanshi/p/7708264.html