任务28:RequestDelegate管道实现思路

任务28:RequestDelegate管道实现思路

管道的实现机制

RequestDelegate是管道的核心。ApplicationBuilder就是接收了很多个RequestDelegae把它拼到一起。

 

第一种方式接受一个Context和next,next就是RequestDeletgate。这种方式是没有返回值的

下面这种方式是接受一个RequestDelegate,返回了一个RequestDelegate。

接收一个next自己没有做什么事情,把自己要做的事情封装成了一个Function交给别人,也就是我们的ApplicationBuilder来负责处理。

 

接下就去看一下我们的ApplicationBuilder,它是如何在你注册之后,我通过Build的方式构建整个管道的

看asp.net core的源代码

这里接收一个RequestDelegate

 

最终的Build的方法

ApplicationBuilder的源代码

https://github.com/aspnet/AspNetCore/blob/master/src/Http/Http/src/Internal/ApplicationBuilder.cs

最后进行了反转

 

RunExtensions的Run方法

https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.aspnetcore.builder.runextensions.run?view=aspnetcore-2.0

 

posted @ 2019-02-12 21:25  高山-景行  阅读(925)  评论(0编辑  收藏  举报