public class HelloWorldOptions
    {
        public HelloWorldOptions()
        {
            IncludeTimestamp = true;
            Name = "World";
        }

        public bool IncludeTimestamp { get; set; }
        public string Name { get; set; }
    }

    public class HelloWorldComponent
    {
        readonly Func<IDictionary<string, object>, Task> _next;
        readonly HelloWorldOptions _options;

        public HelloWorldComponent(Func<IDictionary<string, object>, Task> next, HelloWorldOptions options)
        {
            _next = next;
            _options = options;
        }

        public async Task Invoke(IDictionary<string, object> environment)
        {
            var response = environment["owin.ResponseBody"] as Stream;
            using (var writer = new StreamWriter(response))
            {
                if (_options.IncludeTimestamp)
                {
                    await writer.WriteAsync(DateTime.Now.ToLongTimeString());
                }
                await writer.WriteAsync("Hello, " + _options.Name + "!");
            }
        }
    }

    public static class AppBuilderExtensions
    {
        public static void UseHelloWorld(
            this IAppBuilder app, HelloWorldOptions options = null)
        {
            options = options ?? new HelloWorldOptions();
            app.Use<HelloWorldComponent>(options);
        }
    }
//app.UseHelloWorld(new HelloWorldOptions
            //{
            //    IncludeTimestamp = true,
            //    Name = "Earth"
            //});

 

相关文章:

  • 2021-11-06
  • 2021-06-16
  • 2021-05-23
  • 2022-12-23
  • 2022-03-05
  • 2021-05-28
  • 2022-02-03
猜你喜欢
  • 2022-12-23
  • 2022-01-29
  • 2021-10-29
  • 2021-08-23
  • 2022-12-23
  • 2021-06-24
  • 2021-12-18
相关资源
相似解决方案