• 将参数传递给ASP.NET Core 2.0中的中间件


    问题

    在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. }

     
  • 相关阅读:
    Steps to Writing Well----Reading Notes
    How to Improve Reading Skills
    Requirement-Driven Linux Shell Programming
    Linux tar command usage
    MVC和MVVM模型
    js中特殊的宏任务
    js 超浓缩 双向绑定
    JavaScript 中的遍历详解
    多段动画整合为一个动画的思路
    Js事件循环(Event Loop)机制
  • 原文地址:https://www.cnblogs.com/tianfengcc/p/7864789.html
Copyright © 2020-2023  润新知