ASP.NET Core 中如何给中间件传参数(转载)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ASP.NET Core 中如何给中间件传参数(转载),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3848字,纯文字阅读大概需要6分钟。
内容图文
![ASP.NET Core 中如何给中间件传参数(转载)](/upload/InfoBanner/zyjiaocheng/853/61af41e0e29045cebfafb88af2d6501f.jpg)
Passing Parameters to Middleware in ASP.NET Core 2.0
Problem
How do you pass parameters to middleware during its setup in ASP.NET Core.
Solution
In an empty project, add a POCO class to hold parameters for the middleware:
public class GreetingOptions { public string GreetAt { get; set; } public string GreetTo { get; set; } }
Add a middleware:
public class GreetingMiddleware { private readonly RequestDelegate next; private readonly GreetingOptions options; public GreetingMiddleware( RequestDelegate next, GreetingOptions options) { this.next = next; this.options = options; } public async Task Invoke( HttpContext context) { var message = $"Good {this.options.GreetAt} {this.options.GreetTo}"; await context.Response.WriteAsync(message); } }
Solution A: Instance Type
Add extension method to configure the middleware:
public static IApplicationBuilder UseGreeting( this IApplicationBuilder app, GreetingOptions options) { return app.UseMiddleware<GreetingMiddleware>(options); }
Configure the middleware:
public void Configure( IApplicationBuilder app, IHostingEnvironment env) { app.UseGreeting(new GreetingOptions { GreetAt = "Morning", GreetTo = "Tahir" }); }
Solution B: Function Type
Add extension method to configure the middleware:
public static IApplicationBuilder UseGreeting( this IApplicationBuilder app, Action<GreetingOptions> configureOptions) { var options = new GreetingOptions(); configureOptions(options); return app.UseMiddleware<GreetingMiddleware>(options); }
Configure the middleware:
public void Configure( IApplicationBuilder app, IHostingEnvironment env) { app.UseGreeting(options => { options.GreetAt = "Morning"; options.GreetTo = "Tahir"; }); }
Discussion
I discussed in an earlier post that it is good practice to define middleware in a separate class and add to the pipeline using extension methods. We may also need to pass information to our middleware classes though and I’ve come across two patterns for this when digging into ASP.NET Core source code and other samples online.
These are really straight forward as demonstrated in solution A and B above. We wrap our parameters in a POCO class and create an extension method that takes in either:
- POCO instance
- Function to call, which in turn sets up the POCO.
Note: The POCO is passed on to the middleware in its constructor. UseMiddleware() method takes in params object[] for arguments to be passed onto middleware constructor.
Configuring Services
These patterns could also be used for setting up dependency injection in the service container. To demonstrate, add a service:
public class MessageService : IMessageService { private readonly MessageOptions options; public MessageService(MessageOptions options) { this.options = options; } public string FormatMessage(string message) { // use options return this.options.Format == MessageFormat.None ? message : this.options.Format == MessageFormat.Upper ? message.ToUpper() : message.ToLower(); } }
Add either of these extension methods to configure services:
// Instance Type public static IServiceCollection AddMessageFormatter( this IServiceCollection services, MessageOptions options) { return services.AddScoped<IMessageService>(factory => { return new MessageService(options); }); } // Function Type public static IServiceCollection AddMessageFormatter( this IServiceCollection services, Action<MessageOptions> configureOptions) { var options = new MessageOptions(); configureOptions(options); return services.AddScoped<IMessageService>(factory => { return new MessageService(options); }); }
Configure services with either of these:
// Instance Type public void ConfigureServices( IServiceCollection services) { services.AddMessageFormatter(new MessageOptions { Format = MessageFormat.Lower }); } // Function Type public void ConfigureServices( IServiceCollection services) { services.AddMessageFormatter(options => { options.Format = MessageFormat.Lower; }); }
内容总结
以上是互联网集市为您收集整理的ASP.NET Core 中如何给中间件传参数(转载)全部内容,希望文章能够帮你解决ASP.NET Core 中如何给中间件传参数(转载)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。