zoukankan      html  css  js  c++  java
  • 构建RequestDelegate管道

    1. 创建 Context.cs

    using System;
    using System.Threading.Tasks;
    
    namespace MyPipeline
    {
       public class Context{
           
       }
    }

    2. 创建 RequestDelegate.cs

    using System;
    using System.Threading.Tasks;
    
    namespace MyPipeline
    {
       public delegate Task RequestDelegate(Context context);
    }

    3. 具体实现

    using System; 
    using System.Collections.Generic; 
    using System.Threading.Tasks; 
    
    namespace MyPipeline {
        class Program {
    
            public static List < Func < RequestDelegate, RequestDelegate >> _list = new List < Func < RequestDelegate, RequestDelegate >> (); 
            static void Main(string[] args) {
                
               Use(next =>  {
                    return context =>  {
                        System.Console.WriteLine("1"); 
                    return  next.Invoke(context); 
                    }; 
                }); 
    
                Use(next =>  {
                    return context =>  {
                        System.Console.WriteLine("2"); 
                    return  next.Invoke(context); 
                    }; 
                }); 
    
    
                RequestDelegate end = (context) =>  {
                            System.Console.WriteLine("end ... "); 
                                return Task.CompletedTask; 
    
                }; 
    
                _list.Reverse(); 
                foreach (var middleware in _list) {
                    end = middleware.Invoke(end); 
    
                }
    
                end.Invoke(new Context()); 
    
                Console.ReadLine(); 
    
            }
    
    
            public static void Use(Func < RequestDelegate, RequestDelegate > middleware) {
                _list.Add(middleware); 
            }
        }
    }

    4. 运行结果

    1
    2
    end ...
  • 相关阅读:
    阅读笔记《梦断代码》其一
    第一次冲刺(第九天)
    第一次冲刺(第八天)
    第一冲刺阶段(第七天)
    第一冲刺阶段(第六天)
    第一冲刺阶段(第五天)
    MySQL数据库半同步复制
    MySQL事物
    MySQL字符编码
    MySQL用户授权
  • 原文地址:https://www.cnblogs.com/sunxuchu/p/9247141.html
Copyright © 2011-2022 走看看