简单的Asp.net core管道模拟

网友投稿 586 2022-11-24

简单的Asp- core管道模拟

简单的Asp- core管道模拟

简单的管道模拟

using System;using System.Collections;using System.Collections.Generic;using System.Threading.Tasks;using static ConsolePipeline.Program;using System.Linq;namespace ConsolePipeline{ public class Program { public delegate Task RequestDelegate(Httpcontext static void Main(string[] args) { ApplicationBuilder app = new ApplicationBuilder(); app.Use(async (context, next) => { Console.WriteLine("第一次 开始。。。"); await next(); Console.WriteLine("第一次 结束。。。"); }); app.Use(async (context, next) => { Console.WriteLine("第二次 开始。。。"); await next(); Console.WriteLine("第二次 结束。。。"); }); var firstmiddleware = app.Build(); firstmiddleware(new Httpcontext()); Console.WriteLine("Hello World!"); } } public class Httpcontext { } public class ApplicationBuilder { //中间的委托,不是中间件 public static readonly IList> _components = new List>(); //原生Use public ApplicationBuilder Use(Func middleware) { _components.Add(middleware); return this; } // 扩展Use public ApplicationBuilder Use(Func, Task> middleware) { return Use(next => { return context => { Task SimpleNext() => next(context); return middleware(context, SimpleNext); }; }); } public RequestDelegate Build() { RequestDelegate app = next => { Console.WriteLine("中间中间件。。。"); return Task.CompletedTask; }; foreach (var component in _components.Reverse()) { app = component(app); } return app; } }}

运行效果:

龙腾一族至尊龙骑

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:线程取消
下一篇:【笔记分享】`Cell`与`RefCell`的关联与差别
相关文章

 发表评论

暂时没有评论,来抢沙发吧~