using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Xml; using WeOnlineApp.Configuration; using IwbZero.ToolCommon.StringModel; using Swashbuckle.Swagger; namespace WeOnlineApp.SwaggerUi { /// /// swagger显示控制器的描述 /// public class SwaggerControllerDescProvider : ISwaggerProvider { private readonly ISwaggerProvider _swaggerProvider; private static ConcurrentDictionary _cache = new ConcurrentDictionary(); private readonly string _xml; /// /// /// /// /// xml文档路径 public SwaggerControllerDescProvider(ISwaggerProvider swaggerProvider, string xml) { _swaggerProvider = swaggerProvider; _xml = xml; } public SwaggerDocument GetSwagger(string rootUrl, string apiVersion) { var cacheKey = $"{rootUrl}_{apiVersion}"; //只读取一次 if (!_cache.TryGetValue(cacheKey, out var srcDoc)) { srcDoc = _swaggerProvider.GetSwagger(rootUrl, apiVersion); srcDoc.vendorExtensions = new Dictionary { { "ControllerDesc", GetControllerDesc() } }; _cache.TryAdd(cacheKey, srcDoc); } return srcDoc; } /// /// 从API文档中读取控制器描述 /// /// 所有控制器描述 public ConcurrentDictionary GetControllerDesc() { string xmlPath = _xml; ConcurrentDictionary controllerDescDict = new ConcurrentDictionary(); if (File.Exists(xmlPath)) { XmlDocument xDoc = new XmlDocument(); xDoc.Load(xmlPath); // ReSharper disable once PossibleNullReferenceException foreach (XmlNode node in xDoc.SelectNodes("//member")) { if (node.Attributes != null) { var type = node.Attributes["name"].Value; if (type.StartsWith("T:")) { //控制器 var arrPath = type.Split('.'); var length = arrPath.Length; var controllerName = arrPath[length - 1]; string key = ""; var summaryNode = node.SelectSingleNode("summary"); if (controllerName.EndsWith("Controller")) { //获取控制器注释 int cCount = "Controller".Length; key = controllerName.Remove(controllerName.Length - cCount, cCount); } else if (controllerName.EndsWith("AppService")) { //获取AppService注释 int cCount = "AppService".Length; key = controllerName.Remove(controllerName.Length - cCount, cCount); } else if (controllerName.EndsWith("ApplicationService")) { //获取ApplicationService注释 int cCount = "ApplicationService".Length; key = controllerName.Remove(controllerName.Length - cCount, cCount); } if (key.IsNotEmpty()) { key = $"{IwbConsts.ServicePrefix}95{key.Substring(0, 1).ToLowerInvariant() + key.Substring(1)}"; if (summaryNode != null && !string.IsNullOrEmpty(summaryNode.InnerText) && !controllerDescDict.ContainsKey(key)) { controllerDescDict.TryAdd(key, summaryNode.InnerText.Trim()); } } } } } } return controllerDescDict; } } }