问题

在ASP.NET Core的安装过程中,如何将参数传递给中间件?

在一个空的项目中添加一个POCO类来保存中间件的参数,

  1. publicclass GreetingOptions

  2. {

  3. public string GreetAt { get; set; }

  4. public string GreetTo { get; set; }

  5. }

添加一个中间件,

  1. publicclass GreetingMiddleware

  2. {

  3. private readonly RequestDelegate next;

  4. private readonly GreetingOptions options;

  5. public GreetingMiddleware(

  6. RequestDelegate next,

  7. GreetingOptions options)

  8. {

  9. this.next = next;

  10. this.options = options;

  11. }

  12. public async Task Invoke(

  13. HttpContext context)

  14. {

  15. var message = $"Good {this.options.GreetAt} {this.options.GreetTo}";

  16. await context.Response.WriteAsync(message);

  17. }

  18. }

解决方案A - 实例类型

添加扩展方法来配置中间件,

  1. publicstatic IApplicationBuilder UseGreeting(

  2. this IApplicationBuilder app, GreetingOptions options)

  3. {

  4. return app.UseMiddleware<GreetingMiddleware>(options);

  5. }

配置中间件,

  1. publicvoid Configure(

  2. IApplicationBuilder app,

  3. IHostingEnvironment env)

  4. {

  5. app.UseGreeting(new GreetingOptions

  6. {

  7. GreetAt = "Morning",

  8. GreetTo = "Tahir"

  9. });

  10. }

解决方案B - 功能类型

添加扩展方法来配置中间件,

  1. publicstatic IApplicationBuilder UseGreeting(

  2. this IApplicationBuilder app, Action<GreetingOptions> configureOptions)

  3. {

  4. var options = new GreetingOptions();

  5. configureOptions(options);

  6. return app.UseMiddleware<GreetingMiddleware>(options);

  7. }

配置中间件,

  1. publicvoid Configure(

  2. IApplicationBuilder app,

  3. IHostingEnvironment env)

  4. {

  5. app.UseGreeting(options =>

  6. {

  7. options.GreetAt = "Morning";

  8. options.GreetTo = "Tahir";

  9. });

  10. }

讨论

我在之前的文章 中讨论过, 在独立的类中定义中间件并使用扩展方法添加到管道中是一种好的做法 。虽然我们也可能需要将信息传递给我们的中间件类,但是在深入挖掘ASP.NET Core源代码和其他样本时,我已经遇到了两种模式。

如上面的解决方案A和B所证明的那样,这是非常简单的。我们将我们的参数包装在一个POCO类中,并创建一个扩展方法,

  1. POCO实例

  2. 函数调用,进而建立POCO。

请注意

POCO传递给构造函数中的中间件。UseMiddleware()方法需要params对象[]参数传递到中间件构造函数。

配置服务

这些模式也可以用来设置 服务容器的依赖注入。为了演示添加服务,

  1. publicclass MessageService : IMessageService

  2. {

  3. private readonly MessageOptions options;

  4. public MessageService(MessageOptions options)

  5. {

  6. this.options = options;

  7. }

  8. public string FormatMessage(string message)

  9. {

  10. // use options

  11. returnthis.options.Format == MessageFormat.None ? message :

  12. this.options.Format == MessageFormat.Upper ? message.ToUpper() :

  13. message.ToLower();

  14. }

  15. }

添加这些扩展方法来配置服务,

  1. // Instance Type

  2. publicstatic IServiceCollection AddMessageFormatter(

  3. this IServiceCollection services, MessageOptions options)

  4. {

  5. return services.AddScoped<IMessageService>(factory =>

  6. {

  7. returnnew MessageService(options);

  8. });

  9. }

  10. // Function Type

  11. publicstatic IServiceCollection AddMessageFormatter(

  12. this IServiceCollection services, Action<MessageOptions> configureOptions)

  13. {

  14. var options = new MessageOptions();

  15. configureOptions(options);

  16. return services.AddScoped<IMessageService>(factory =>

  17. {

  18. returnnew MessageService(options);

  19. });

  20. }

使用其中之一配置服务,

  1. // Instance Type

  2. publicvoid ConfigureServices(

  3. IServiceCollection services)

  4. {

  5. services.AddMessageFormatter(new MessageOptions

  6. {

  7. Format = MessageFormat.Lower

  8. });

  9. }

  10. // Function Type

  11. publicvoid ConfigureServices(

  12. IServiceCollection services)

  13. {

  14. services.AddMessageFormatter(options =>

  15. {

  16. options.Format = MessageFormat.Lower;

  17. });

  18. }

 

将参数传递给ASP.NET Core 2.0中的中间件的更多相关文章

  1. 基础教程:上传/下载ASP.NET Core 2.0中的文件

    问题 如何上传和下载ASP.NET Core MVC中的文件. 解 在一个空的项目中,更新 Startup 类以添加MVC的服务和中间件. publicvoid ConfigureServices( ...

  2. 避免在ASP.NET Core 3.0中为启动类注入服务

    本篇是如何升级到ASP.NET Core 3.0系列文章的第二篇. Part 1 - 将.NET Standard 2.0类库转换为.NET Core 3.0类库 Part 2 - IHostingE ...

  3. Jmeter----A接口response中body的某一个参数传递给B接口request的body中使用(参数的传递)

    示例:将接口"获取待办列表"response中body的id值传递给接口"删除待办"request的body中使用: 操作步骤如下: 第一步:给"获取 ...

  4. 在ASP.NET Core 2.0中使用CookieAuthentication

    在ASP.NET Core中关于Security有两个容易混淆的概念一个是Authentication(认证),一个是Authorization(授权).而前者是确定用户是谁的过程,后者是围绕着他们允 ...

  5. 如何在ASP.NET Core 2.0中使用Razor页面

    如何在ASP.NET Core 2.0中使用Razor页面  DotNetCore2017-11-22 14:49 问题 如何在ASP.NET Core 2.0中使用Razor页面 解 创建一个空的项 ...

  6. ASP.NET Core 3.0中使用动态控制器路由

    原文:Dynamic controller routing in ASP.NET Core 3.0 作者:Filip W 译文:https://www.cnblogs.com/lwqlun/p/114 ...

  7. 探索 ASP.Net Core 3.0系列三:ASP.Net Core 3.0中的Service provider validation

    前言:在本文中,我将描述ASP.NET Core 3.0中新的“validate on build”功能. 这可以用来检测您的DI service provider是否配置错误. 具体而言,该功能可检 ...

  8. ASP.NET Core 2.0 中读取 Request.Body 的正确姿势

    原文:ASP.NET Core 中读取 Request.Body 的正确姿势 ASP.NET Core 中的 Request.Body 虽然是一个 Stream ,但它是一个与众不同的 Stream ...

  9. ASP.NET Core 1.0 中的依赖项管理

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

随机推荐

  1. Oracle ADF VO排序及VO的查询模式

    常规应用中,当需要使用Table向终端用户展示数据时,Table中数据的显示排序一致性极大程度的影响到了客户体验.通常希望诸如多次查询结果显示顺序相同.插入数据在原数据上方等的实现. ADF为开发人员 ...

  2. Golang基本结构之练习(day2)

    笔记: . 任何一个代码文件隶属于一个包 . import 关键字,引用其他包: import(“fmt”) import(“os”) 通常习惯写成: import ( “fmt” “os” ) . ...

  3. Windows c++程序的基本结构

    Windows c++程序的基本结构 1.一个完整的Windows应用程序通常由五种类型的文件组成 C语言源程序文件 头文件 模块定义文件 资源描述文件 项目文件 2.Windows应用程序构成基本框 ...

  4. Debian Mount nfs 出错的解决

    系统未安装 nfs 客户端 #aptitude install nfs-common 解决!

  5. html符号转换

    通常情况下,HTML会自动截去多余的空格.不管你加多少空格,都被看做一个空格.比如你在两个字之间加了10个空格,HTML会截去9个空格,只保留一个.为了在网页中增加空格,你可以使用 表示空格.最常用的 ...

  6. 【原创】请避免GO语言中的携程空跑(CPU突然激增)

    其实GO语言从1.6版本开始非常不错了,GC性能优化非常到位,并且各种并行设计比从新实现一套C++版本的确是方便不少. 语言包也很多,库也相对稳定,完全可以适用于生产环境. 本文主要是给刚刚入门新手注 ...

  7. Basic4android v3.00 发布

    这次发布的版本主要是增加了快速debuger. 在运行时,可以在IDE 里面随时修改代码,而不需要重新发布应用. 大大提高了开发效率. Basic4android v3.00 is released. ...

  8. mongo学习-固定集合

    一.创建固定集合 db.createCollection("guding",{"capped":true,"size":10,"m ...

  9. 一)get started with the Quartz project

    官网 http://www.quartz-scheduler.org/ 下载链接 http://www.terracotta.org/download/reflector.jsp?b=tcdistri ...

  10. java并发编程实战:第十六章----Java内存模型

    一.什么是内存模型,为什么要使用它 如果缺少同步,那么将会有许多因素使得线程无法立即甚至永远看到一个线程的操作结果 编译器把变量保存在本地寄存器而不是内存中 编译器中生成的指令顺序,可以与源代码中的顺 ...