owin中间件

    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"
            //});
原文地址:https://www.cnblogs.com/shiningrise/p/5561263.html