DOTNET 中间件 (自定义) 类似 asp.net core 中的中间件

直接上代码比较好

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace src
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            IApplicationBuilder app = new ApplicationBuilder();

            app.Use(async (context, next) =>
            {
                context.SeyHello();
                await next.Invoke();
            });

            app.Use(async (context, next) =>
            {
                context.Sey("你好");
                await next.Invoke();
            });

            var _task = app.Build();

            var context = new NetworkContext();
            var context1 = new NetworkContext();

            await _task(context);

            await _task(context1);
        }
    }

    public class ApplicationBuilder : IApplicationBuilder
    {
        private readonly IList<Func<NetworkRequestDelegate, NetworkRequestDelegate>> _middlewares = new List<Func<NetworkRequestDelegate, NetworkRequestDelegate>>();
        public NetworkRequestDelegate Build()
        {
            NetworkRequestDelegate app = context =>
            {
                return Task.CompletedTask;
            };

            for (var c = _middlewares.Count - 1; c >= 0; c--)
            {
                app = _middlewares[c](app);
            }

            return app;
        }

        public IApplicationBuilder New()
        {
            return new ApplicationBuilder();
        }

        public IApplicationBuilder Use(Func<NetworkRequestDelegate, NetworkRequestDelegate> middleware)
        {
            _middlewares.Add(middleware);
            return this;
        }
    }

    public interface IApplicationBuilder
    {
        IApplicationBuilder Use(Func<NetworkRequestDelegate, NetworkRequestDelegate> middleware);

        NetworkRequestDelegate Build();

        IApplicationBuilder New();

    }

    public class NetworkContext
    {
        private Guid _id;

        public NetworkContext()
        {
            _id = Guid.NewGuid();
        }

        public void SeyHello()
        {
            Console.WriteLine($"{_id} say hello.");
        }

        public void Sey(string msg)
        {
            Console.WriteLine($"{_id} say {msg}");
        }
    }

    public delegate Task NetworkRequestDelegate(NetworkContext context);

    public static class UseExtensions
    {
        public static IApplicationBuilder Use(this IApplicationBuilder app, Func<NetworkContext, Func<Task>, Task> middleware)
        {
            return app.Use(next =>
            {
                return context =>
                {
                    Func<Task> simpleNext = () => next(context);
                    return middleware(context, simpleNext);
                };
            });
        }

        public static IApplicationBuilder Use(this IApplicationBuilder app, Func<NetworkContext, NetworkRequestDelegate, Task> middleware)
        {
            return app.Use(next => context => middleware(context, next));
        }

    }

    public static class RunExtensions
    {
        public static void Run(this IApplicationBuilder app, NetworkRequestDelegate handler)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            app.Use(_ => handler);
        }
    }
}

原文地址:https://www.cnblogs.com/microestc/p/15341806.html