Asp.Net Core EndPoint 终点路由工作原理解读

一、背景

在本打算写一篇关于Identityserver4 的文章时候,却发现自己对EndPoint -终结点路由还不是很了解,故暂时先放弃了IdentityServer4 的研究和编写;所以才产生了今天这篇关于EndPoint (终结点路由) 的文章。

还是跟往常一样,打开电脑使用强大的Google 和百度搜索引擎查阅相关资料,以及打开Asp.net core 3.1 的源代码进行拜读,同时终于在我的实践及测试中对EndPoint 有了不一样的认识,说到这里更加敬佩微软对Asp.net core 3.x 的框架中管道模型的设计。

我先来提出以下几个问题:

1.当访问一个Web 应用地址时,Asp.Net Core 是怎么执行到Controller 的Action的呢?2.Endpoint 跟普通路由又存在着什么样的关系?3.UseRouing() 、UseAuthorization()UserEndpoints() 这三个中间件的关系是什么呢?4.怎么利用Endpoint 编写自己的中间件以及Endpoint 的应用场景(时间有限,下回分享整理)

二、拜读源码解惑

Startup 代码

我们先来看一下Startup中简化版的代码,代码如下:

public void ConfigureServices(IServiceCollection services)
{services.AddControllers();
}public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{app.UseRouting();app.UseAuthorization();app.UseEndpoints(endpoints =>{endpoints.MapControllers();});
}

程序启动阶段:

•第一步:执行services.AddControllers() 将Controller的核心服务注册到容器中去•第二步:执行app.UseRouting() 将EndpointRoutingMiddleware中间件注册到http管道中•第三步:执行app.UseAuthorization() 将AuthorizationMiddleware中间件注册到http管道中•第四步:执行app.UseEndpoints(encpoints=>endpoints.MapControllers()) 有两个主要的作用:调用endpoints.MapControllers()将本程序集定义的所有ControllerAction转换为一个个的EndPoint放到路由中间件的配置对象RouteOptions中 将EndpointMiddleware中间件注册到http管道中

app.UseRouting() 源代码如下:

public static IApplicationBuilder UseRouting(this IApplicationBuilder builder)
{if (builder == null){throw new ArgumentNullException(nameof(builder));}VerifyRoutingServicesAreRegistered(builder);var endpointRouteBuilder = new DefaultEndpointRouteBuilder(builder);builder.Properties[EndpointRouteBuilder] = endpointRouteBuilder;return builder.UseMiddleware<EndpointRoutingMiddleware>(endpointRouteBuilder);}

EndpointRoutingMiddleware 中间件代码如下:

internal sealed class EndpointRoutingMiddleware{private const string DiagnosticsEndpointMatchedKey = "Microsoft.AspNetCore.Routing.EndpointMatched";private readonly MatcherFactory _matcherFactory;private readonly ILogger _logger;private readonly EndpointDataSource _endpointDataSource;private readonly DiagnosticListener _diagnosticListener;private readonly RequestDelegate _next;private Task<Matcher> _initializationTask;public EndpointRoutingMiddleware(MatcherFactory matcherFactory,ILogger<EndpointRoutingMiddleware> logger,IEndpointRouteBuilder endpointRouteBuilder,DiagnosticListener diagnosticListener,RequestDelegate next){if (endpointRouteBuilder == null){throw new ArgumentNullException(nameof(endpointRouteBuilder));}_matcherFactory = matcherFactory ?? throw new ArgumentNullException(nameof(matcherFactory));_logger = logger ?? throw new ArgumentNullException(nameof(logger));_diagnosticListener = diagnosticListener ?? throw new ArgumentNullException(nameof(diagnosticListener));_next = next ?? throw new ArgumentNullException(nameof(next));_endpointDataSource = new CompositeEndpointDataSource(endpointRouteBuilder.DataSources);}public Task Invoke(HttpContext httpContext){// There's already an endpoint, skip maching completelyvar endpoint = httpContext.GetEndpoint();if (endpoint != null){Log.MatchSkipped(_logger, endpoint);return _next(httpContext);}// There's an inherent race condition between waiting for init and accessing the matcher// this is OK because once `_matcher` is initialized, it will not be set to null again.var matcherTask = InitializeAsync();if (!matcherTask.IsCompletedSuccessfully){return AwaitMatcher(this, httpContext, matcherTask);}var matchTask = matcherTask.Result.MatchAsync(httpContext);if (!matchTask.IsCompletedSuccessfully){return AwaitMatch(this, httpContext, matchTask);}return SetRoutingAndContinue(httpContext);// Awaited fallbacks for when the Tasks do not synchronously completestatic async Task AwaitMatcher(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task<Matcher> matcherTask){var matcher = await matcherTask;await matcher.MatchAsync(httpContext);await middleware.SetRoutingAndContinue(httpContext);}static async Task AwaitMatch(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matchTask){await matchTask;await middleware.SetRoutingAndContinue(httpContext);}}[MethodImpl(MethodImplOptions.AggressiveInlining)]private Task SetRoutingAndContinue(HttpContext httpContext){// If there was no mutation of the endpoint then log failurevar endpoint = httpContext.GetEndpoint();if (endpoint == null){Log.MatchFailure(_logger);}else{// Raise an event if the route matchedif (_diagnosticListener.IsEnabled() && _diagnosticListener.IsEnabled(DiagnosticsEndpointMatchedKey)){// We're just going to send the HttpContext since it has all of the relevant information_diagnosticListener.Write(DiagnosticsEndpointMatchedKey, httpContext);}Log.MatchSuccess(_logger, endpoint);}return _next(httpContext);}// Initialization is async to avoid blocking threads while reflection and things// of that nature take place.//// We've seen cases where startup is very slow if we  allow multiple threads to race// while initializing the set of endpoints/routes. Doing CPU intensive work is a// blocking operation if you have a low core count and enough work to do.private Task<Matcher> InitializeAsync(){var initializationTask = _initializationTask;if (initializationTask != null){return initializationTask;}return InitializeCoreAsync();}private Task<Matcher> InitializeCoreAsync(){var initialization = new TaskCompletionSource<Matcher>(TaskCreationOptions.RunContinuationsAsynchronously);var initializationTask = Interlocked.CompareExchange(ref _initializationTask, initialization.Task, null);if (initializationTask != null){// This thread lost the race, join the existing task.return initializationTask;}// This thread won the race, do the initialization.try{var matcher = _matcherFactory.CreateMatcher(_endpointDataSource);// Now replace the initialization task with one created with the default execution context.// This is important because capturing the execution context will leak memory in ASP.NET Core.using (ExecutionContext.SuppressFlow()){_initializationTask = Task.FromResult(matcher);}// Complete the task, this will unblock any requests that came in while initializing.initialization.SetResult(matcher);return initialization.Task;}catch (Exception ex){// Allow initialization to occur again. Since DataSources can change, it's possible// for the developer to correct the data causing the failure._initializationTask = null;// Complete the task, this will throw for any requests that came in while initializing.initialization.SetException(ex);return initialization.Task;}}private static class Log{private static readonly Action<ILogger, string, Exception> _matchSuccess = LoggerMessage.Define<string>(LogLevel.Debug,new EventId(1, "MatchSuccess"),"Request matched endpoint '{EndpointName}'");private static readonly Action<ILogger, Exception> _matchFailure = LoggerMessage.Define(LogLevel.Debug,new EventId(2, "MatchFailure"),"Request did not match any endpoints");private static readonly Action<ILogger, string, Exception> _matchingSkipped = LoggerMessage.Define<string>(LogLevel.Debug,new EventId(3, "MatchingSkipped"),"Endpoint '{EndpointName}' already set, skipping route matching.");public static void MatchSuccess(ILogger logger, Endpoint endpoint){_matchSuccess(logger, endpoint.DisplayName, null);}public static void MatchFailure(ILogger logger){_matchFailure(logger, null);}public static void MatchSkipped(ILogger logger, Endpoint endpoint){_matchingSkipped(logger, endpoint.DisplayName, null);}}}

我们从它的源码中可以看到,EndpointRoutingMiddleware中间件先是创建matcher,然后调用matcher.MatchAsync(httpContext)去寻找Endpoint,最后通过httpContext.GetEndpoint()验证了是否已经匹配到了正确的Endpoint并交个下个中间件继续执行!

app.UseEndpoints() 源代码

public static IApplicationBuilder UseEndpoints(this IApplicationBuilder builder, Action<IEndpointRouteBuilder> configure)
{if (builder == null){throw new ArgumentNullException(nameof(builder));}if (configure == null){throw new ArgumentNullException(nameof(configure));}VerifyRoutingServicesAreRegistered(builder);VerifyEndpointRoutingMiddlewareIsRegistered(builder, out var endpointRouteBuilder);configure(endpointRouteBuilder);// Yes, this mutates an IOptions. We're registering data sources in a global collection which// can be used for discovery of endpoints or URL generation.//// Each middleware gets its own collection of data sources, and all of those data sources also// get added to a global collection.var routeOptions = builder.ApplicationServices.GetRequiredService<IOptions<RouteOptions>>();foreach (var dataSource in endpointRouteBuilder.DataSources){routeOptions.Value.EndpointDataSources.Add(dataSource);}return builder.UseMiddleware<EndpointMiddleware>();
}internal class DefaultEndpointRouteBuilder : IEndpointRouteBuilder
{public DefaultEndpointRouteBuilder(IApplicationBuilder applicationBuilder){ApplicationBuilder = applicationBuilder ?? throw new ArgumentNullException(nameof(applicationBuilder));DataSources = new List<EndpointDataSource>();}public IApplicationBuilder ApplicationBuilder { get; }public IApplicationBuilder CreateApplicationBuilder() => ApplicationBuilder.New();public ICollection<EndpointDataSource> DataSources { get; }public IServiceProvider ServiceProvider => ApplicationBuilder.ApplicationServices;}

代码中构建了DefaultEndpointRouteBuilder 终结点路由构建者对象,该对象中存储了Endpoint的集合数据;同时把终结者路由集合数据存储在了routeOptions 中,并注册了EndpointMiddleware 中间件到http管道中; Endpoint对象代码如下:

/// <summary>
/// Represents a logical endpoint in an application.
/// </summary>
public class Endpoint
{/// <summary>/// Creates a new instance of <see cref="Endpoint"/>./// </summary>/// <param name="requestDelegate">The delegate used to process requests for the endpoint.</param>/// <param name="metadata">/// The endpoint <see cref="EndpointMetadataCollection"/>. May be null./// </param>/// <param name="displayName">/// The informational display name of the endpoint. May be null./// </param>public Endpoint(RequestDelegate requestDelegate,EndpointMetadataCollection metadata,string displayName){// All are allowed to be nullRequestDelegate = requestDelegate;Metadata = metadata ?? EndpointMetadataCollection.Empty;DisplayName = displayName;}/// <summary>/// Gets the informational display name of this endpoint./// </summary>public string DisplayName { get; }/// <summary>/// Gets the collection of metadata associated with this endpoint./// </summary>public EndpointMetadataCollection Metadata { get; }/// <summary>/// Gets the delegate used to process requests for the endpoint./// </summary>public RequestDelegate RequestDelegate { get; }public override string ToString() => DisplayName ?? base.ToString();}

Endpoint 对象代码中有两个关键类型属性分别是EndpointMetadataCollection 类型和RequestDelegate

•EndpointMetadataCollection:存储了Controller 和Action相关的元素集合,包含Action 上的Attribute 特性数据等•RequestDelegate :存储了Action 也即委托,这里是每一个Controller 的Action 方法

再回过头来看看EndpointMiddleware 中间件和核心代码,EndpointMiddleware 的一大核心代码主要是执行Endpoint 的RequestDelegate 委托,也即Controller 中的Action 的执行。

public Task Invoke(HttpContext httpContext)
{var endpoint = httpContext.GetEndpoint();if (endpoint?.RequestDelegate != null){if (!_routeOptions.SuppressCheckForUnhandledSecurityMetadata){if (endpoint.Metadata.GetMetadata<IAuthorizeData>() != null &&!httpContext.Items.ContainsKey(AuthorizationMiddlewareInvokedKey)){ThrowMissingAuthMiddlewareException(endpoint);}if (endpoint.Metadata.GetMetadata<ICorsMetadata>() != null &&!httpContext.Items.ContainsKey(CorsMiddlewareInvokedKey)){ThrowMissingCorsMiddlewareException(endpoint);}}Log.ExecutingEndpoint(_logger, endpoint);try{var requestTask = endpoint.RequestDelegate(httpContext);if (!requestTask.IsCompletedSuccessfully){return AwaitRequestTask(endpoint, requestTask, _logger);}}catch (Exception exception){Log.ExecutedEndpoint(_logger, endpoint);return Task.FromException(exception);}Log.ExecutedEndpoint(_logger, endpoint);return Task.CompletedTask;}return _next(httpContext);static async Task AwaitRequestTask(Endpoint endpoint, Task requestTask, ILogger logger){try{await requestTask;}finally{Log.ExecutedEndpoint(logger, endpoint);}}
}

疑惑解答:

1. 当访问一个Web 应用地址时,Asp.Net Core 是怎么执行到Controller 的Action的呢?

答:程序启动的时候会把所有的Controller 中的Action 映射存储到routeOptions 的集合中,Action 映射成Endpoint终结者 的RequestDelegate 委托属性,最后通过UseEndPoints 添加EndpointMiddleware 中间件进行执行,同时这个中间件中的Endpoint 终结者路由已经是通过Rouing匹配后的路由。

2. EndPoint 跟普通路由又存在着什么样的关系?

答:Ednpoint 终结者路由是普通路由map 转换后的委托路由,里面包含了路由方法的所有元素信息EndpointMetadataCollection 和RequestDelegate 委托。

3. UseRouing() 、UseAuthorization()UseEndpoints() 这三个中间件的关系是什么呢?

答:UseRouing 中间件主要是路由匹配,找到匹配的终结者路由Endpoint ;UseEndpoints 中间件主要针对UseRouing 中间件匹配到的路由进行 委托方法的执行等操作。 UseAuthorization 中间件主要针对 UseRouing 中间件中匹配到的路由进行拦截 做授权验证操作等,通过则执行下一个中间件UseEndpoints(),具体的关系可以看下面的流程图:

上面流程图中省略了一些部分,主要是把UseRouing 、UseAuthorization 、UseEndpoint 这三个中间件的关系突显出来。

以上如果有错误的地方,请大家积极纠正,谢谢大家的支持!!

扫描二维码

获取更多精彩

长按关注

Asp.Net Core EndPoint 终结点路由工作原理解读相关推荐

  1. Asp.Net Core Authorize解析(源码解读)

    一.前言 IdentityServer4已经分享了一些应用实战的文章,从架构到授权中心的落地应用,也伴随着对IdentityServer4掌握了一些使用规则,但是很多原理性东西还是一知半解,故我这里持 ...

  2. 使用SWAGGER和ASP.NET CORE设置可选路由参数

    使用SWAGGER和ASP.NET CORE设置可选路由参数 根据OpenAPI 3.0,这是不可能的.但是,如果您真的希望成为现实呢?您是否必须解决并允许您的Swagger文档出错?我在这里向您展示 ...

  3. 路由工作原理+DHCP+静态路由配置

    路由工作原理+DHCP+静态路由配置 路由器 路由表 路由优先级 路由优先级的配置 浮动路由 路由的度量 DHCP DHCP第一种配置命令(global): DHCP第二种分配IP地址的配置命令(in ...

  4. 前端路由工作原理与使用

    单页应用和多页应用 单页面应用:所有功能在一个页面上实现 一个.html 文件 前端路由 组件化开发 网易云音乐 小米移动端 多页应用:与单页应用相对应的,不同的功能通过不同的页面来实现 单页面 - ...

  5. asp.net core 3.x Endpoint终结点路由1-基本介绍和使用

    前言 我是从.net 4.5直接跳到.net core 3.x的,感觉asp.net这套东西最初是从4.5中的owin形成的. 目前官方文档重点是讲路由,没有特别说明与传统路由的区别,本篇主要介绍终结 ...

  6. 【ASP.NET Core】给路由规则命名有何用处

    上一篇中老周给伙伴们介绍了自定义视图搜索路径的方法,本篇咱们扯一下有关 URL 路径规则的名称问题.在扯今天的话题之前,先补充点东东.在上一篇中设置视图搜索路径时用到三个有序参数:{2}{1}{0}, ...

  7. ASP.Net Core Razor 页面路由

    在服务器端 Web 应用程序框架中,其中非常重要的设计是开发人员如何将URL与服务器上的资源进行匹配,以便正确的处理请求.最简单的方法是将 URL 映射到磁盘上的物理文件,在 Razor 页面框架中, ...

  8. 第14节 单臂路由工作原理及简单实验

    单臂路由 1引言 2单臂路由 2.1路由器子接口 2.2工作原理 3单臂路由实验 4总结 1引言 在前两节课的学习中,我们了解了VLAN及Trunk的作用,不同VLAN之间虽没有广播需求但仍有单播通信 ...

  9. ElasticSearch工作原理解读及一些思考

    一.概述 在此之前,一直想写关于 ES 相关的文章,但是工作实在太忙,没能抽空完成,今天刚好有时间,那我们就先来总结一下 ES 相关的知识点,学习一个新东西一定要先从整体去看全局,然后再到局部去了解细 ...

最新文章

  1. 预训练是AI未来所需要的全部吗?
  2. 关于LUA+Unity开发_toLua篇【二】
  3. Hello Android – 迈出android开发第一步
  4. 1.10 字符串的替换(replace()、replaceFirst()和replaceAll())
  5. map area 鼠标跟随
  6. redis php扩展 linux,linux下为php安装redis扩展phpredis
  7. IO概述(概念分类)
  8. C#操作Excel总结
  9. 库卡机器人C4计算机无法启动,KUKA-C4标准版机器人启动时序
  10. Java数组在方法区吗,Java数组的操作方法
  11. [Android] SharedPreference的使用
  12. html做偶像图片2048小游戏,一个自制的2048小游戏(一)
  13. unity2d 投影_Unity Projector 投影器原理以及优化
  14. 解决no session问题的三种方式
  15. Google I/O:谷歌AR看似不紧不慢,实则暗藏玄机
  16. Latex 伪代码、三线表与多线表
  17. (10.2.3.3)静电的设计教室:APP设计利器Sketch教程(03)-让插件助你一臂之力(原创
  18. norflash的基本操作2
  19. 关于Python打包文件的步骤
  20. 北斗时钟服务器(NTP服务器)让高考时间更加精准

热门文章

  1. 简单模拟实现简单的当登录延时的效果
  2. matlab练习程序(PCASVD)
  3. Android---AlarmManager(全局定时器/闹钟)指定时长或以周期形式执行某项操作
  4. 基于'sessionStorage'与'userData'的类session存储
  5. plex实现流媒体服务器_如何从Plex Media Server离线查看下载和同步媒体
  6. t-mobile频段_T-Mobile再次被黑客入侵:超过200万个帐号和地址可能泄漏
  7. Linux就该这么学---第七章(LVM逻辑卷管理器)
  8. # 20172307 2018-2019-1 《程序设计与数据结构》第5周学习总结
  9. [原创]同一个Tomcat,配置多个context、多个Host
  10. Linux和Windows下部署BeetleX服务网关