将参数传递给ASP.NET Core 2.0中的中间件
问题
在ASP.NET Core的安装过程中,如何将参数传递给中间件?
解
在一个空的项目中添加一个POCO类来保存中间件的参数,
-
publicclass GreetingOptions
-
{
-
public string GreetAt { get; set; }
-
public string GreetTo { get; set; }
-
}
添加一个中间件,
-
publicclass 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);
-
}
-
}
解决方案A - 实例类型
添加扩展方法来配置中间件,
-
publicstatic IApplicationBuilder UseGreeting(
-
this IApplicationBuilder app, GreetingOptions options)
-
{
-
return app.UseMiddleware<GreetingMiddleware>(options);
-
}
配置中间件,
-
publicvoid Configure(
-
IApplicationBuilder app,
-
IHostingEnvironment env)
-
{
-
app.UseGreeting(new GreetingOptions
-
{
-
GreetAt = "Morning",
-
GreetTo = "Tahir"
-
});
-
}
解决方案B - 功能类型
添加扩展方法来配置中间件,
-
publicstatic IApplicationBuilder UseGreeting(
-
this IApplicationBuilder app, Action<GreetingOptions> configureOptions)
-
{
-
var options = new GreetingOptions();
-
configureOptions(options);
-
return app.UseMiddleware<GreetingMiddleware>(options);
-
}
配置中间件,
-
publicvoid Configure(
-
IApplicationBuilder app,
-
IHostingEnvironment env)
-
{
-
app.UseGreeting(options =>
-
{
-
options.GreetAt = "Morning";
-
options.GreetTo = "Tahir";
-
});
-
}
讨论
我在之前的文章 中讨论过, 在独立的类中定义中间件并使用扩展方法添加到管道中是一种好的做法 。虽然我们也可能需要将信息传递给我们的中间件类,但是在深入挖掘ASP.NET Core源代码和其他样本时,我已经遇到了两种模式。
如上面的解决方案A和B所证明的那样,这是非常简单的。我们将我们的参数包装在一个POCO类中,并创建一个扩展方法,
-
POCO实例
-
函数调用,进而建立POCO。
请注意
POCO传递给构造函数中的中间件。UseMiddleware()方法需要params对象[]参数传递到中间件构造函数。
配置服务
这些模式也可以用来设置 服务容器的依赖注入。为了演示添加服务,
-
publicclass MessageService : IMessageService
-
{
-
private readonly MessageOptions options;
-
public MessageService(MessageOptions options)
-
{
-
this.options = options;
-
}
-
public string FormatMessage(string message)
-
{
-
// use options
-
returnthis.options.Format == MessageFormat.None ? message :
-
this.options.Format == MessageFormat.Upper ? message.ToUpper() :
-
message.ToLower();
-
}
-
}
添加这些扩展方法来配置服务,
-
// Instance Type
-
publicstatic IServiceCollection AddMessageFormatter(
-
this IServiceCollection services, MessageOptions options)
-
{
-
return services.AddScoped<IMessageService>(factory =>
-
{
-
returnnew MessageService(options);
-
});
-
}
-
// Function Type
-
publicstatic IServiceCollection AddMessageFormatter(
-
this IServiceCollection services, Action<MessageOptions> configureOptions)
-
{
-
var options = new MessageOptions();
-
configureOptions(options);
-
return services.AddScoped<IMessageService>(factory =>
-
{
-
returnnew MessageService(options);
-
});
-
}
使用其中之一配置服务,
-
// Instance Type
-
publicvoid ConfigureServices(
-
IServiceCollection services)
-
{
-
services.AddMessageFormatter(new MessageOptions
-
{
-
Format = MessageFormat.Lower
-
});
-
}
-
// Function Type
-
publicvoid ConfigureServices(
-
IServiceCollection services)
-
{
-
services.AddMessageFormatter(options =>
-
{
-
options.Format = MessageFormat.Lower;
-
});
-
}