ASP.NET Core 2.0 带初始参数的中间件问题及解决方法
问题
如何在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")); }); }
运行,此时页面显示:
点击下载源码:http://xiazai.jb51.net/201710/yuanma/middlewarewithparameters.rar
总结
以上所述是小编给大家介绍的asp.net core 2.0 带初始参数的中间件问题及解决方法,希望对大家有所帮助
上一篇: PHP新特性之字节码缓存和内置服务器