任务29:自己动手构建RequestDelegate管道
2024-08-30 12:16:47
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("");
return next.Invoke(context);
//return Task.CompletedTask;
};
});
Use(next=>{
return context=>{
Console.WriteLine("");
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管道的构成过程
最新文章
- Linux实战教学笔记03:操作系统发展历程及系统版本选择
- Redis安装及实现session共享
- JSP页面静态化
- 【android】Android am命令使用
- jQuery整体架构源码解析(转载)
- C#自旋的艺术
- Hadoop阅读笔记(六)——洞悉Hadoop序列化机制Writable
- datepicker冲突
- JS 显示时间与倒计时练习
- C#中如何设置窗体的默认按钮和取消按钮
- solr5.5教程-schema.xml部分配置
- github Top100
- Linux常用命令--网络管理篇(三)
- Windows Phone 8本地化多语言支持
- VK Cup 2017 - Round 1
- postgresql分析函数
- Elasticsearch之删除索引
- JavaScript——this
- 安装MongDB
- ubi 文件系统加载失败原因记录
热门文章
- OHIFViewer meteor build 问题
- 【转】Linux cp -a用法
- HDOJ 1217 Arbitrage(拟最短路,floyd算法)
- HTML URL编码规则
- JS 计算2个日期相差的天数
- 怎样搭建svn本地server,管理本地的代码
- Statelessness Provide credentials with the request. Each request MUST stand alone and should not be affected from previous conversation happened from same client in past.
- MFC上显示摄像头JPEG图片数据的两种方法
- DDD领域建模基本流程
- iOS开发UIScrollView常见属性和方法