您好,登录后才能下订单哦!
小编给大家分享一下如何在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。