问题
如何在ASP.NET Core 2.0向中间件传入初始参数?
答案
在一个空项目中,创建一个POCO(Plain Old CLR Object)来保存中间件所需的参数:
1
2
3
4
5
|
public class GreetingOptions
{
public string GreetAt { get ; set ; }
public string GreetTo { get ; set ; }
}
|
添加一个中间件:
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:实例类型
添加一个扩展方法来配置中间件:
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:函数类型
添加一个扩展方法来配置中间件:
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" ;
});
}
|
上述两种方法结果一致。
运行,此时页面显示:
讨论
之前我们曾讨论过,在单独的类中定义中间件并使用扩展方法将其添加到请求管道中是最佳实践。我们也可能需要向中间件传入参数,通过对ASP.NET Core源代码以及其他在线示例的学习,我总结出来上面两种模式。
上述的两种解决方法都非常直观。我们将参数封装到一个POCO类中,然后创建一个扩展方法来接受下面的参数:
1. POCO实例
2. 需要调用的函数(在函数内设置POCO)
注:POCO实例通过构造函数传入中间件。UseMiddleware()方法接收可变参数params object[],并将这些参数传入中间件构造函数。
配置服务
这些模式也能用于向服务容器中添加服务实例。为了便于说明,我们先添加一个服务:
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}" ;
}
}
|
添加如下任一个扩展方法来配置服务:
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()中使用此服务:
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" ;
});
}
|
因为ConfigureServices()先于Configure()执行,因此我们可以直接在Configure()注入此服务:
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" ));
});
}
|
运行,此时页面显示:
点击下载源码:MiddlewareWithParameters.rar
总结
以上所述是小编给大家介绍的ASP.NET Core 2.0 带初始参数的中间件问题及解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/sanshi/p/7708264.html