zoukankan      html  css  js  c++  java
  • 29-自己动手构建RequestDelegate管道

    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-显示结果为

  • 相关阅读:
    hdu-4638
    一个简单的询问
    CoderForces-617B
    HYSBZ-2002弹飞绵羊
    邻接表
    CoderForces-913-C
    CoderForces-913D
    CoderFocers-620C
    CoderForces-375D
    HDU-6119
  • 原文地址:https://www.cnblogs.com/qinzb/p/9346095.html
Copyright © 2011-2022 走看看