在上篇文章中,我为构建自定义端点可视化图奠定了基础,正如我在第一篇文章中展示的那样。该图显示了端点路由的不同部分:文字值,参数,动词约束和产生结果的端点:

在本文中,我将展示如何通过创建一个自定义的DfaGraphWriter来为自己的应用程序创建一个端点图。

这篇文章使用了本系列前几篇文章中的技巧和类,因此我强烈建议在继续之前先阅读这些技巧和类。

相关阅读:

[译]使用DOT语言和GraphvizOnline来可视化你的ASP.NETCore3.0终结点01

将终结点图添加到你的ASP.NET Core应用程序中

使用ImpromptuInterface反射方便的创建自定义DfaGraphWriter

为端点图添加配置

我们首先要看的是如何配置最终端点图的外观。我们将为两种类型的节点和四种类型的边缘添加配置。边是:

  • 文字边缘:路线部分,例如apivalues中的文字匹配api/values/{id}

  • 参数边缘:路线的参数化部分,例如{id}route中api/values/{id}

  • 捕获所有边:与“全部捕获”路由参数相对应的边,例如{**slug}

  • 策略边缘:与URL以外的其他约束相对应的边缘。例如,图中的基于HTTP谓词的边HTTP: GET

节点是:

  • 匹配节点:与端点匹配关联的节点,因此将生成响应。

  • 默认节点:与端点匹配关联的节点。

每个节点和边都可以具有任意数量的Graphviz属性来控制其显示。下面的GraphDisplayOptions显示了我在本文开始时用于生成图形的默认值:

public class GraphDisplayOptions
{/// <summary>/// Additional display options for literal edges/// </summary>public string LiteralEdge { get; set; } = string.Empty;/// <summary>/// Additional display options for parameter edges/// </summary>public string ParametersEdge { get; set; } = "arrowhead=diamond color=\"blue\"";/// <summary>/// Additional display options for catchall parameter edges/// </summary>public string CatchAllEdge { get; set; } = "arrowhead=odot color=\"green\"";/// <summary>/// Additional display options for policy edges/// </summary>public string PolicyEdge { get; set; } = "color=\"red\" style=dashed arrowhead=open";/// <summary>/// Additional display options for node which contains a match/// </summary>public string MatchingNode { get; set; } = "shape=box style=filled color=\"brown\" fontcolor=\"white\"";/// <summary>/// Additional display options for node without matches/// </summary>public string DefaultNode { get; set; } = string.Empty;
}

我们现在可以使用这个对象来控制显示,并使用上一篇文章中所示的ImpromptuInterface“代理”技术来创建我们的自定义图形编写器。

创建自定义的DfaGraphWriter

我们的自定义图形编辑器(巧妙地称为CustomDfaGraphWriter)在很大程度上基于包含在ASP.NET Core中的DfaGraphWriter。该类的主体与原始类相同,但有以下更改:

  • GraphDisplayOptions注入类中以自定义显示。

  • 使用ImpromptuInterface库来处理内部DfaMatcherBuilderDfaNode类,如上一篇文章中所示。

  • 自定义WriteNode函数以使用我们的自定义样式。

  • 添加一个Visit函数来处理IDfaNode接口,而不是在内部DfaNode类上使用Visit()方法。

CustomDfaGraphWriter的全部代码如下所示,重点是主Write()功能。我保持了与原始版本几乎相同的实现,只是更新了我们必须更新的部分。

public class CustomDfaGraphWriter
{// Inject the GraphDisplayOptionsprivate readonly IServiceProvider _services;private readonly GraphDisplayOptions _options;public CustomDfaGraphWriter(IServiceProvider services, GraphDisplayOptions options){_services = services;_options = options;}public void Write(EndpointDataSource dataSource, TextWriter writer){// Use ImpromptuInterface to create the required dependencies as shown in previous postType matcherBuilder = typeof(IEndpointSelectorPolicy).Assembly.GetType("Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder");// Build the list of endpoints used to build the graphvar rawBuilder = _services.GetRequiredService(matcherBuilder);IDfaMatcherBuilder builder = rawBuilder.ActLike<IDfaMatcherBuilder>();// This is the same logic as the original graph writervar endpoints = dataSource.Endpoints;for (var i = 0; i < endpoints.Count; i++){if (endpoints[i] is RouteEndpoint endpoint && (endpoint.Metadata.GetMetadata<ISuppressMatchingMetadata>()?.SuppressMatching ?? false) == false){builder.AddEndpoint(endpoint);}}// Build the raw tree from the registered routesvar rawTree = builder.BuildDfaTree(includeLabel: true);IDfaNode tree = rawTree.ActLike<IDfaNode>();// Store a list of nodes that have already been visitedvar visited = new Dictionary<IDfaNode, int>();// Build the graph by visiting each node, and calling WriteNode on eachwriter.WriteLine("digraph DFA {");Visit(tree, WriteNode);writer.WriteLine("}");void WriteNode(IDfaNode node){/* Write the node to the TextWriter *//* Details shown later in this post*/}}static void Visit(IDfaNode node, Action<IDfaNode> visitor){/* Recursively visit each node in the tree. *//* Details shown later in this post*/}
}

为了简洁起见,我在这里省略了Visit和 WriteNode函数,但是我们会尽快对其进行研究。我们将从Visit函数开始,因为该函数最接近原始函数。

更新Visit函数以与IDfaNode一起使用

正如我在上一篇文章中所讨论的,创建自定义DfaGraphWriter的最大问题之一是它对内部类的使用。为了解决这个问题,我使用ImpromptuInterface创建了包装原始对象的代理对象:

原始的Visit()方法是DfaNode类中的方法。它递归地访问端点树中的每个节点,为每个节点调用一个提供的Action<>函数。

由于DfaNodeinternal,我在CustomDfaGraphWriter中实现了一个静态的Visit来代替。

我们的定制实现大体上与原始实现相同,但是我们必须在“原始”DfaNodes和我们的IDfaNode代理之间进行一些有点困难的转换。更新后的方法如下所示。该方法接受两个参数,即被检查的节点,以及在每个参数上运行的Action<>

static void Visit(IDfaNode node, Action<IDfaNode> visitor)
{// Does the node of interest have any nodes connected by literal edges?if (node.Literals?.Values != null){// node.Literals is actually a Dictionary<string, DfaNode>foreach (var dictValue in node.Literals.Values){// Create a proxy for the child DfaNode node and visit itIDfaNode value = dictValue.ActLike<IDfaNode>();Visit(value, visitor);}}// Does the node have a node connected by a parameter edge?// The reference check breaks any cycles in the graphif (node.Parameters != null && !ReferenceEquals(node, node.Parameters)){// Create a proxy for the DfaNode node and visit itIDfaNode parameters = node.Parameters.ActLike<IDfaNode>();Visit(parameters, visitor);}// Does the node have a node connected by a catch-all edge?// The refernece check breaks any cycles in the graphif (node.CatchAll != null && !ReferenceEquals(node, node.CatchAll)){// Create a proxy for the DfaNode node and visit itIDfaNode catchAll = node.CatchAll.ActLike<IDfaNode>();Visit(catchAll, visitor);}// Does the node have a node connected by a policy edges?if (node.PolicyEdges?.Values != null){// node.PolicyEdges is actually a Dictionary<object, DfaNode>foreach (var dictValue in node.PolicyEdges.Values){IDfaNode value = dictValue.ActLike<IDfaNode>();Visit(value, visitor);}}// Write the node using the provided Action<>visitor(node);
}

Visit函数使用post-order遍历,因此在使用visitor函数编写节点之前,它首先“深入”地遍历节点的子节点。这与原始DfaNode.Visit()功能相同。

我们现在快到了。我们有一个类,它构建端点节点树,遍历树中的所有节点,并为每个节点运行一个函数。剩下的就是定义访问者函数WriteNode()

定义自定义WriteNode函数

我们终于到了最重要的部分,控制了端点图的显示方式。到目前为止,所有自定义和努力都是使我们能够自定义WriteNode功能。

WriteNode()是一个局部函数,它使用点图描述语言将一个节点连同任何连接的边一起写入TextWriter输出。

我们的自定义WriteNode()函数与原始函数几乎相同。有两个主要区别:

  • 原始的图形编写器使用DfaNodes,我们必须转换为使用IDfaNode代理。

  • 原始图形编写器对所有节点和边使用相同的样式。我们根据配置的GraphDisplayOptions定制节点和边的显示。

由于WriteNode是一个局部函数,它可以从封闭函数访问变量。这包括writer参数(用于将图形写入输出)和以前写入节点的已访问字典。

下面显示了我们的方法(已被大量注释)的自定义版本WriteNode()

void WriteNode(IDfaNode node)
{// add the node to the visited node dictionary if it isn't already// generate a zero-based integer label for the nodeif (!visited.TryGetValue(node, out var label)){label = visited.Count;visited.Add(node, label);}// We can safely index into visited because this is a post-order traversal,// all of the children of this node are already in the dictionary.// If this node is linked to any nodes by a literal edgeif (node.Literals != null){foreach (DictionaryEntry dictEntry in node.Literals){// Foreach linked node, get the label for the edge and the linked nodevar edgeLabel = (string)dictEntry.Key;IDfaNode value = dictEntry.Value.ActLike<IDfaNode>();int nodeLabel = visited[value];// Write an edge, including our custom styling for literal edgeswriter.WriteLine($"{label} -> {nodeLabel} [label=\"/{edgeLabel}\" {_options.LiteralEdge}]");}}// If this node is linked to a nodes by a parameter edgeif (node.Parameters != null){IDfaNode parameters = node.Parameters.ActLike<IDfaNode>();int nodeLabel = visited[catchAll];// Write an edge labelled as /* using our custom styling for parameter edgeswriter.WriteLine($"{label} -> {nodeLabel} [label=\"/**\" {_options.CatchAllEdge}]");}// If this node is linked to a catch-all edgeif (node.CatchAll != null && node.Parameters != node.CatchAll){IDfaNode catchAll = node.CatchAll.ActLike<IDfaNode>();int nodeLabel = visited[catchAll];// Write an edge labelled as /** using our custom styling for catch-all edgeswriter.WriteLine($"{label} -> {nodelLabel} [label=\"/**\" {_options.CatchAllEdge}]");}// If this node is linked to any Policy Edgesif (node.PolicyEdges != null){foreach (DictionaryEntry dictEntry in node.PolicyEdges){// Foreach linked node, get the label for the edge and the linked nodevar edgeLabel = (object)dictEntry.Key;IDfaNode value = dictEntry.Value.ActLike<IDfaNode>();int nodeLabel = visited[value];// Write an edge, including our custom styling for policy edgeswriter.WriteLine($"{label} -> {nodeLabel} [label=\"{key}\" {_options.PolicyEdge}]");}}// Does this node have any associated matches, indicating it generates a response?var matchCount = node?.Matches?.Count ?? 0;var extras = matchCount > 0 ? _options.MatchingNode // If we have matches, use the styling for response-generating nodes...: _options.DefaultNode; // ...otherwise use the default style// Write the node to the graph outputwriter.WriteLine($"{label} [label=\"{node.Label}\" {extras}]");
}

由于我们将节点从“叶”节点写回到树的根的方式,因此跟踪这些交互的流程可能会有些混乱。例如,如果我们看一下本文开头显示的基本应用程序的输出,您会看到“叶子”端点都被首先写入:healthz运行状况检查端点和终端匹配生成路径最长的端点:

digraph DFA {1 [label="/healthz/" shape=box style=filled color="brown" fontcolor="white"]2 [label="/api/Values/{...}/ HTTP: GET" shape=box style=filled color="brown" fontcolor="white"]3 [label="/api/Values/{...}/ HTTP: PUT" shape=box style=filled color="brown" fontcolor="white"]4 [label="/api/Values/{...}/ HTTP: DELETE" shape=box style=filled color="brown"  fontcolor="white"]5 [label="/api/Values/{...}/ HTTP: *" shape=box style=filled color="brown" fontcolor="white"]6 -> 2 [label="HTTP: GET" color="red" style=dashed arrowhead=open]6 -> 3 [label="HTTP: PUT" color="red" style=dashed arrowhead=open]6 -> 4 [label="HTTP: DELETE" color="red" style=dashed arrowhead=open]6 -> 5 [label="HTTP: *" color="red" style=dashed arrowhead=open]6 [label="/api/Values/{...}/"]7 [label="/api/Values/ HTTP: GET" shape=box style=filled color="brown" fontcolor="white"]8 [label="/api/Values/ HTTP: POST" shape=box style=filled color="brown" fontcolor="white"]9 [label="/api/Values/ HTTP: *" shape=box style=filled color="brown" fontcolor="white"]10 -> 6 [label="/*" arrowhead=diamond color="blue"]10 -> 7 [label="HTTP: GET" color="red" style=dashed arrowhead=open]10 -> 8 [label="HTTP: POST" color="red" style=dashed arrowhead=open]10 -> 9 [label="HTTP: *" color="red" style=dashed arrowhead=open]10 [label="/api/Values/"]11 -> 10 [label="/Values"]11 [label="/api/"]12 -> 1 [label="/healthz"]12 -> 11 [label="/api"]12 [label="/"]
}

即使首先将叶节点写入图形输出,但Graphviz可视化工具通常会以叶节点在底部,边缘朝下的方式绘制图形。您可以在https://dreampuf.github.io/GraphvizOnline/在线显示图形:

如果要更改图形的呈现方式,可以自定义GraphDisplayOptions。如果使用我在上一篇文章中描述的“测试”方法,则可以在生成图形时直接传递这些选项。如果使用的是“中间件”方法,则可以改为使用IOptions<>系统进行GraphDisplayOptions注册,并使用配置系统控制显示。

摘要

在这篇文章中,我展示了如何创建自定义的DfaGraphWriter来控制如何生成应用程序的端点图。为了与internal内部类进行互操作,我们使用了ImpromptuInterface,如在上篇文章所示,创建代理,我们可以互动。然后,我们必须编写一个自定义Visit()函数来使用IDfaNode代理。最后,我们创建了一个自定义WriteNode函数,该函数使用在GraphDisplayOptions对象中定义的自定义设置来显示不同类型的节点和边。

往期精彩回顾

【推荐】.NET Core开发实战视频课程 ★★★

.NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划

【.NET Core微服务实战-统一身份认证】开篇及目录索引

Redis基本使用及百亿数据量中的使用技巧分享(附视频地址及观看指南)

.NET Core中的一个接口多种实现的依赖注入与动态选择看这篇就够了

10个小技巧助您写出高性能的ASP.NET Core代码

用abp vNext快速开发Quartz.NET定时任务管理界面

在ASP.NET Core中创建基于Quartz.NET托管服务轻松实现作业调度

现身说法:实际业务出发分析百亿数据量下的多表查询优化

关于C#异步编程你应该了解的几点建议

C#异步编程看这篇就够了

给我好看
您看此文用  · 秒,转发只需1秒呦~
好看你就点点我

在ASP.NET Core中创建自定义端点可视化图相关推荐

  1. 如何在ASP.NET Core中创建自定义AuthorizeAttribute?

    本文翻译自:How do you create a custom AuthorizeAttribute in ASP.NET Core? I'm trying to make a custom aut ...

  2. ASP.NET Core 配置 - 创建自定义配置提供程序

    ASP.NET Core 配置 - 创建自定义配置提供程序 在本文中,我们将创建一个自定义配置提供程序,从数据库读取我们的配置.我们已经了解了默认配置提供程序的工作方式,现在我们将实现我们自己的自定义 ...

  3. 【半译】在ASP.NET Core中创建内部使用作用域服务的Quartz.NET宿主服务

    在我的上一篇文章<在ASP.NET Core中创建基于Quartz.NET托管服务轻松实现作业调度>,我展示了如何使用ASP.NET Core创建Quartz.NET托管服务并使用它来按计 ...

  4. 如何在ASP.NET Core中编写自定义日志记录提供程序

    目录 介绍 如何实现所需的接口 基础类和附件 FileLoggerProvider具体类及其附件 1. ConfigureLogging() 2. appsettings.json文件 介绍 源代码可 ...

  5. ASP.NET Core中显示自定义错误页面-增强版

    之前的博文 ASP.NET Core中显示自定义错误页面 中的方法是在项目中硬编码实现的,当有多个项目时,就会造成不同项目之间的重复代码,不可取. 在这篇博文中改用middleware实现,并且放在独 ...

  6. 在ASP.NET Core中创建基于Quartz.NET托管服务轻松实现作业调度

    在这篇文章中,我将介绍如何使用ASP.NET Core托管服务运行Quartz.NET作业.这样的好处是我们可以在应用程序启动和停止时很方便的来控制我们的Job的运行状态.接下来我将演示如何创建一个简 ...

  7. 在 ASP.NET MVC 中创建自定义 HtmlHelper

    在ASP.NET MVC应用程序的开发中,我们常碰到类似Html.Label或Html.TextBox这样的代码,它将在网页上产生一个label或input标记.这些HtmlHelper的扩展方法有些 ...

  8. asp.net nancy_如何在ASP.Net Core中使用Nancy

    asp.net nancy Nancy是用于构建基于HTTP的服务的轻量级框架. Nancy更喜欢约定而不是配置,并提供对GET,HEAD,POST,PUT,DELETE和PATCH操作的支持. Na ...

  9. 如何在ASP.Net Core中使用Nancy

    Nancy是用于构建基于HTTP的服务的轻量级框架. Nancy更喜欢约定而不是配置,并提供对GET,HEAD,POST,PUT,DELETE和PATCH操作的支持. Nancy在MIT许可下也是开源 ...

最新文章

  1. 嵌入式开发之davinci--- ipnc中用到的cmem
  2. Java中的单例模式
  3. [转]Win下必备神器之Cmder
  4. 满足条件的两个数或多个数
  5. C语言实现AVL树(附完整源码)
  6. exp/imp三种模式——完全、用户、表
  7. html css基础知识
  8. 解决:pycharm运行程序时在Python console窗口中运行 һ����ң�������1�����
  9. BAT架构师进阶:大型网站架构书籍推荐
  10. ACM模板——差分约束
  11. Java 设计模式之 Abstract Factory 抽象工厂模式
  12. 真不是炼丹,务实敢为的 MoCo v3
  13. mysql8 距离计算_MySQL8 的 Hash join 算法
  14. react怎么引入jquery_在react里面使用jquery插件
  15. Vue+Vant的组件自定义使用阿里图标库图标
  16. 如何在Win10安装“Siemens TIA Openness”
  17. C语言信息学一本通题库答案,信息学奥赛一本通(C++版)在线评测系统
  18. cpu占用高 mongo_记一次 MongoDB 占用 CPU 过高问题的排查
  19. 输出pdf文件的一个简单方法
  20. 晶体三极管结构及其工作原理详解

热门文章

  1. MVC中提交表单的4种方式
  2. Git 常用命令(二)
  3. 配置FTP服务2(vsftpd 配置虚拟账号)
  4. 了解jQuery技巧来提高你的代码
  5. linux文件句柄,【LINUX】使用lsof处理文件恢复、句柄以及空间释放问题
  6. 三、Python-列表
  7. productFlavors设置signingConfig不管用的问题
  8. SDWebImage 4 0 迁移指南
  9. 如何离线安装chrome插件
  10. HTML form的一些属性(第一版)