• ASP.NET Core Middleware管道介绍


            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                app.Use(next => {
                    return async context => {
                        await context.Response.WriteAsync("A11111111</br>");
                        await next.Invoke(context); //下一中间件
                        await context.Response.WriteAsync("B11111111</br>");
                    };
                });
                app.Use(next => {
                    return async context => {
                        await context.Response.WriteAsync("A22222222</br>");
                        await next.Invoke(context); //下一中间件
                        await context.Response.WriteAsync("B22222222</br>");
                    };
                });
                app.Use(next => {
                    return async context => {
                        await context.Response.WriteAsync("A33333333</br>");
                        await context.Response.WriteAsync("B33333333</br>");
                    };
                });
    
            }

     

    运行的时候是先执行3->2->1 倒序

    自己动手构建RequestDelegate管道

        class Program
        {
            static void Main(string[] args)
            {
                ApplicationBuilder app = new ApplicationBuilder();
                app.Use(next =>
                {
                    return async context =>
                    {
                        await context.WriteAsync("A11111111
    ");
                        await next.Invoke(context); //下一中间件
                        await context.WriteAsync("B11111111
    ");
                    };
                });
                app.Use(next =>
                {
                    return async context =>
                    {
                        await context.WriteAsync("A22222222
    ");
                        await next.Invoke(context); //下一中间件
                        await context.WriteAsync("B22222222
    ");
                    };
                });
                app.Use(next =>
                {
                    return async context =>
                    {
                        await context.WriteAsync("A33333333
    ");
                        await context.WriteAsync("B33333333
    ");
                    };
                });
                var requestDelegate = app.Build();
                requestDelegate.Invoke(new HttpContext());
                Console.ReadLine();
            }
    
            public delegate Task RequestDelegate(HttpContext context);
    
            public class HttpContext
            {
                public async Task WriteAsync(string text)
                {
                    Console.WriteLine(text);
                    await Task.CompletedTask;
                }
            }
    
            public class ApplicationBuilder
            {
                public List<Func<RequestDelegate, RequestDelegate>> _list = new List<Func<RequestDelegate, RequestDelegate>>();
                RequestDelegate requestDelegate = new RequestDelegate(async context =>
                {
                    Console.WriteLine("404
    ");
                    await Task.CompletedTask;
                });
    
                public RequestDelegate Build()
                {
                    _list.Reverse();
                    foreach (var func in _list)
                    {
                        requestDelegate = func.Invoke(requestDelegate);
                    }
                    return requestDelegate;
                }
    
                public ApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware)
                {
                    this._list.Add(middleware);
                    return this;
                }
            }
        }

  • 相关阅读:
    数据中台实战(六):交易分析
    数据中台实战(五):自助分析平台(产品设计篇)
    数据中台实战(四):商品分析(产品设计篇)
    数据中台实战(三):用户分析(产品设计篇)
    数据中台实战(二):基于阿里OneData的数据指标管理体系
    数据中台实战(一):以B2B电商亿订为例,谈谈产品经理视角下的数据埋点
    LeetCode82. 删除排序链表中的重复元素 II
    LeetCode203. 移除链表元素
    LeetCode445. 两数相加 II
    LeetCode2. 两数相加
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/9104339.html
Copyright © 2020-2023  润新知