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

    cmd创建一个控制台应用程序

    dotnet new console --name MyPipeline

    用VSCode打开这个项目

     新建类RequestDelegate.cs的类文件复制Program里面的代码到RequestDelegate里面代码修改

    引入命名空间。

    创建一个delegaet 叫做 RequestDelegate

    为了演示用,新建一个Context.cs

    完善RequestDelegate

    新建一个List接收一个RequestDelegate返回一个Delegate

    public static List<Func<RequestDelegate,RequestDelegate>> _list=new List<Func<RequestDelegate, RequestDelegate>>();

    模拟我们的ApplicationBuilder里面的方法新建Use的方法,接受一个Func里面接受一个RequestDelegate返回一个RequestDelegate

      public static void Use(Func<RequestDelegate, RequestDelegate> middleware)
      {
         _list.Add(middleware);
      }
    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=>{
                        Console.WriteLine("1");
                        return next.Invoke(context);
                         //return Task.CompletedTask;
                    };
                });
                 Use(next=>{
                    return context=>{
                        Console.WriteLine("2");
                        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());
                Console.ReadLine();
            }
    
            public static void Use(Func<RequestDelegate, RequestDelegate> middleware)
            {
                _list.Add(middleware);
            }
    
        }
    }
    Program.cs

     dotnet run 执行

    执行到1 没有再往下执行

    以上就是整个Http管道的构成过程

  • 相关阅读:
    cereal:C++实现的开源序列化库
    随笔
    我们一起成长
    青岛近代历史和文化资料整理
    Codeforces Round #580 (Div. 2)-D. Shortest Cycle(思维建图+dfs找最小环)
    CodeForces
    PAT甲级1151(由前序和中序确定LCA)
    记录使用vs code两天的心得
    AcWing 285. 没有上司的舞会(树形dp入门)
    POJ-3255-Roadblocks(次短路的另一种求法)
  • 原文地址:https://www.cnblogs.com/wangjunwei/p/10367256.html
Copyright © 2011-2022 走看看