将参数传递给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;
});
}
将参数传递给ASP.NET Core 2.0中的中间件的更多相关文章
- 基础教程:上传/下载ASP.NET Core 2.0中的文件
问题 如何上传和下载ASP.NET Core MVC中的文件. 解 在一个空的项目中,更新 Startup 类以添加MVC的服务和中间件. publicvoid ConfigureServices( ...
- 避免在ASP.NET Core 3.0中为启动类注入服务
本篇是如何升级到ASP.NET Core 3.0系列文章的第二篇. Part 1 - 将.NET Standard 2.0类库转换为.NET Core 3.0类库 Part 2 - IHostingE ...
- Jmeter----A接口response中body的某一个参数传递给B接口request的body中使用(参数的传递)
示例:将接口"获取待办列表"response中body的id值传递给接口"删除待办"request的body中使用: 操作步骤如下: 第一步:给"获取 ...
- 在ASP.NET Core 2.0中使用CookieAuthentication
在ASP.NET Core中关于Security有两个容易混淆的概念一个是Authentication(认证),一个是Authorization(授权).而前者是确定用户是谁的过程,后者是围绕着他们允 ...
- 如何在ASP.NET Core 2.0中使用Razor页面
如何在ASP.NET Core 2.0中使用Razor页面 DotNetCore2017-11-22 14:49 问题 如何在ASP.NET Core 2.0中使用Razor页面 解 创建一个空的项 ...
- ASP.NET Core 3.0中使用动态控制器路由
原文:Dynamic controller routing in ASP.NET Core 3.0 作者:Filip W 译文:https://www.cnblogs.com/lwqlun/p/114 ...
- 探索 ASP.Net Core 3.0系列三:ASP.Net Core 3.0中的Service provider validation
前言:在本文中,我将描述ASP.NET Core 3.0中新的“validate on build”功能. 这可以用来检测您的DI service provider是否配置错误. 具体而言,该功能可检 ...
- ASP.NET Core 2.0 中读取 Request.Body 的正确姿势
原文:ASP.NET Core 中读取 Request.Body 的正确姿势 ASP.NET Core 中的 Request.Body 虽然是一个 Stream ,但它是一个与众不同的 Stream ...
- ASP.NET Core 1.0 中的依赖项管理
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
随机推荐
- Golang之实现一个负载均衡算法(随机,轮询)
代码记录 程序结构目录 --------程序包 package balance type Balancer interface { DoBalance([]*Instance, ...string) ...
- 关于java项目中的.classpath文件:
Classpath是Java中的重要概念: 它描述了Java虚拟机在运行一个Class时在哪些路径中加载要运行的类以及运行的类要用到的类 简单的说,就是像操作系统的path 只不过这个classpat ...
- Apache 调用不同的 PHP 版本
# cd /etc/apache2/mods-enabled 建立 /etc/apache2/mods-available/ 之下的 php7.2.load php7.2.conf 两个文件的符号 ...
- Basic4android v3.00 发布
这次发布的版本主要是增加了快速debuger. 在运行时,可以在IDE 里面随时修改代码,而不需要重新发布应用. 大大提高了开发效率. Basic4android v3.00 is released. ...
- Android Studio真机测试
本页内容 1.真机测试好处多 2.开始吧!真机测试 1.真机测试好处多 不久前才开我的Android Studio之旅,就遇上了一个大麻烦——创建的模拟器各种运行出错.尝试了各种解决方法,都没有什么结 ...
- 前端传递对象列表到WebApi
public Int64 objectPOC(JObject jsonWrapper) { dynamic jsonValues = jsonWrapper; JArray jsonInput = j ...
- Redis Quick Start [熟练版]
一.下载解压 wget http://download.redis.io/redis-stable.tar.gztar xvzf redis-stable.tar.gzcd redis-stable ...
- 通过 cygwin64 自己编译对应的 Tera Term cyglaunch.exe
步骤如下: 将 cygterm+.tar.gz解压到任意目录,当然要cygwin容易操作.(本例直接放到$HOME目录下,启动cygwin后的默认目录,如果之前没有更改的话) 将 Makefile 中 ...
- 层层递进Struts1(五)之处理流程
这篇博客我们深入Struts框架执行部分源码,从ActionServlet的process函数开始,看一下其内在的执行过程. 流程图 以下流程图展示的是ActionServlet和RequestPro ...
- CentOS 7.4 shell 不显示当前用户和路径的问题
步骤如下: vim ~/.bash_profile (不用管.bash_profile这个文件有几个,自己新建一个也是可以的) 在最后加上 export PS1='[\u@\h \W]\$' ...