Introduction to middleware with initial parameters in ASP. NET Core 2.0 and asp. netcore
Problem
In ASP. NET Core 2.0, how does one input initial parameters to middleware?
Answer
In an empty project, create a POCO (Plain Old CLR Object) to save the parameters required by 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) { _next = next; _options = options; } public async Task Invoke(HttpContext context) { var message = $"Good {_options.GreetAt} {_options.GreetTo}"; await context.Response.WriteAsync(message); }}
Answer 1: instance type
Add an extension method to configure the middleware:
public static IApplicationBuilder UseGreetingMiddleware(this IApplicationBuilder app, GreetingOptions options){ return app.UseMiddleware<GreetingMiddleware>(options);}
Middleware:
public void Configure(IApplicationBuilder app, IHostingEnvironment env){ app.UseGreetingMiddleware(new GreetingOptions { GreetAt = "Morning", GreetTo = "Tahir" });}
Answer 2: Function Type
Add an extension method to configure the middleware:
public static IApplicationBuilder UseGreetingMiddlewareAction(this IApplicationBuilder app, Action<GreetingOptions> optionsAction){ var options = new GreetingOptions(); optionsAction(options); return app.UseMiddleware<GreetingMiddleware>(options);}
Middleware:
public void Configure(IApplicationBuilder app, IHostingEnvironment env){ app.UseGreetingMiddlewareAction(options => { options.GreetAt = "Morning"; options.GreetTo = "Tahir"; });}
The results of the above two methods are consistent.
Run:
Discussion
We have discussed before that it is best practice to define middleware in a separate class and add it to the request Pipeline Using extension methods. We may also need to input parameters to middleware. By learning the source code of ASP. NET Core and other online examples, I will summarize the above two modes.
The preceding two solutions are intuitive. We encapsulate the parameters in a POCO class and create an extension method to accept the following parameters:
1. POCO instance
2. functions to be called (set POCO in the function)
Note: POCO instances pass in middleware through constructor. The UseMiddleware () method receives the variable parameter params object [] and passes these parameters to the middleware constructor.
Configuration Service
These modes can also be used to add a service instance to a service container. To facilitate the description, we first add a service:
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}"; }}
Add any of the following extension methods to configure the service:
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));}
Use this service in Configure:
public void ConfigureServices(IServiceCollection services){ services.AddMessageService(new GreetingOptions { GreetAt = "Morning", GreetTo = "Tahir" }); services.AddMessageServiceAction(options => { options.GreetAt = "Morning"; options.GreetTo = "Tahir"; });}
Because ConfigureServices () is executed before Configure (), we can directly inject this service in Configure:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMessageService msg){ app.Run(async (context) => { await context.Response.WriteAsync(msg.FormatMessage("by sanshi")); });}
Run:
Source code download
Original article: https://tahirnaushad.com/2017/08/29/passing-parameters-to-middleware-in-asp-net-core-2-0/
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.