首页 > 其他 > 详细

29-自己动手构建RequestDelegate管道

时间:2018-07-21 13:08:40      阅读:447      评论:0      收藏:0      [点我收藏+]

1-使用vsCode新建个项目

技术分享图片

 

2-新建RequestDelegate和Context

    public delegate Task RequestDelegate(Context context);

      public class Context{
        
     }

 

3-Proggram.cs类

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MypipleLine
{
    class Program
    {
        private static List<Func<RequestDelegate,RequestDelegate>> _list = 
        new List<Func<RequestDelegate, RequestDelegate>>();
        static void Main(string[] args)
        {
              Use((next)=>{
                return (context)=>{
                    Console.WriteLine("1111111111");
                    return next.Invoke(context);
                };
            });
            Use((next)=>{
                return (context)=>{
                    Console.WriteLine("222222222");
                    return next.Invoke(context);
                };
            });

            RequestDelegate end = context=>{
                Console.WriteLine("end");
                return Task.CompletedTask;
            };
            
            _list.Reverse();
            foreach(var middleware in _list)
            {
                end = middleware.Invoke(end);
            }

           end.Invoke(new Context());
        }


        static void Use(Func<RequestDelegate,RequestDelegate> middleware){
            _list.Add(middleware);
        }

    }
}

4-显示结果为

技术分享图片

 

29-自己动手构建RequestDelegate管道

原文:https://www.cnblogs.com/qinzb/p/9346095.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!