【译】ASP.NET CORE 2.0 传递参数到中间件

2019-05-17  本文已影响0人  Dino
Asp.net core 2.0

== 原文地址已不能访问,停止本系列更新==

问题

如何传递参数到中间件在构建Asp.net core的时候

解决方案

在空项目中添加POCO类去为中间件持有参数

public class GreetingOptions
    {
        public string GreetAt { get; set; }
        public string GreetTo { get; set; }
    }

添加中间件

 public class 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: 实例类型

添加扩增类型去配置中间件

  public static IApplicationBuilder UseGreeting(
            this IApplicationBuilder app, GreetingOptions options)
        {
            return app.UseMiddleware<GreetingMiddleware>(options);
        }

配置中间件

 public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env)
        {
            app.UseGreeting(new GreetingOptions
            {
                GreetAt = "Morning",
                GreetTo = "Tahir"
            });
        }

解决方案 B: 函数式类型

添加扩增类型去配置中间件

 public static IApplicationBuilder UseGreeting(
            this IApplicationBuilder app, Action<GreetingOptions> configureOptions)
        {
            var options = new GreetingOptions();
            configureOptions(options);

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

配置 中间件

 public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env)
        {
            app.UseGreeting(options =>
            {
                options.GreetAt = "Morning";
                options.GreetTo = "Tahir";
            });
        }

讨论

在上一篇文章中有讨论到定义中间件添加到管道并使用扩增方法是最佳实践,我们可能需要传递参数通过中间件,当我深入研究aspnet core源码和其他例子时发现有两种模式,
这两种模式可以参考上面的解决方案A和解决方案B, 我们包装参数在POCO的类里,创建一个扩增方法传递他们

  1. POCO 实例
  2. 调用函数, 设置 POCO.

注:POCO是传递到中间件的构造函数。 UseMiddleware()params object[] 作为参数传到中间件的构造函数中。

配置服务

这种模式会用到依赖注入到服务容器,演示代码

 public class MessageService : IMessageService
    {
        private readonly MessageOptions options;
 
        public MessageService(MessageOptions options)
        {
            this.options = options;
        }
 
        public string FormatMessage(string message)
        {
            // use options
            return this.options.Format == MessageFormat.None ? message :
                    this.options.Format == MessageFormat.Upper ? message.ToUpper() :
                     message.ToLower();
        }
    }

添加这些扩增方法到配置服务:

 // Instance Type
        public static IServiceCollection AddMessageFormatter(
            this IServiceCollection services, MessageOptions options)
        {
            return services.AddScoped<IMessageService>(factory =>
            {
                return new MessageService(options);
            });
        }
 
        // Function Type
        public static IServiceCollection AddMessageFormatter(
            this IServiceCollection services, Action<MessageOptions> configureOptions)
        {
            var options = new MessageOptions();
            configureOptions(options);
 
            return services.AddScoped<IMessageService>(factory =>
            {
                return new MessageService(options);
            });
        }

用下面的代码配置服务

// Instance Type
        public void ConfigureServices(
            IServiceCollection services)
        {
            services.AddMessageFormatter(new MessageOptions
            {
                Format = MessageFormat.Lower
            });
        }
 
        // Function Type
        public void ConfigureServices(
            IServiceCollection services)
        {
            services.AddMessageFormatter(options =>
            {
                options.Format = MessageFormat.Lower;
            });
        }

Source Code

GitHub: https://github.com/TahirNaushad/Fiver.Asp.Middleware.Options

上一篇下一篇

猜你喜欢

热点阅读