RpcAutoRouteProvider.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #if !NETSTANDARD1_3
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using EdjCase.JsonRpc.Router.Abstractions;
  5. using System;
  6. using System.Reflection;
  7. using EdjCase.JsonRpc.Router.MethodProviders;
  8. using Microsoft.Extensions.Options;
  9. namespace EdjCase.JsonRpc.Router.RouteProviders
  10. {
  11. /// <summary>
  12. /// Default route provider to give the router the configured routes to use
  13. /// </summary>
  14. public class RpcAutoRouteProvider : IRpcRouteProvider
  15. {
  16. public IOptions<RpcAutoRoutingOptions> Options { get; }
  17. public RpcAutoRouteProvider(IOptions<RpcAutoRoutingOptions> options)
  18. {
  19. this.Options = options ?? throw new ArgumentNullException(nameof(options));
  20. }
  21. public RpcPath BaseRequestPath => this.Options.Value.BaseRequestPath;
  22. private Dictionary<RpcPath, List<IRpcMethodProvider>> routeCache { get; set; }
  23. private Dictionary<RpcPath, List<IRpcMethodProvider>> GetAllRoutes()
  24. {
  25. if (this.routeCache == null)
  26. {
  27. //TODO will entry assembly be good enough
  28. List<TypeInfo> controllerTypes = Assembly.GetEntryAssembly().DefinedTypes
  29. .Where(t => !t.IsAbstract && t.IsSubclassOf(this.Options.Value.BaseControllerType))
  30. .ToList();
  31. var controllerRoutes = new Dictionary<RpcPath, List<IRpcMethodProvider>>();
  32. foreach (TypeInfo controllerType in controllerTypes)
  33. {
  34. var attribute = controllerType.GetCustomAttribute<RpcRouteAttribute>(true);
  35. string routePathString;
  36. if (attribute == null || attribute.RouteName == null)
  37. {
  38. if (controllerType.Name.EndsWith("Controller"))
  39. {
  40. routePathString = controllerType.Name.Substring(0, controllerType.Name.IndexOf("Controller"));
  41. }
  42. else
  43. {
  44. routePathString = controllerType.Name;
  45. }
  46. }
  47. else
  48. {
  49. routePathString = attribute.RouteName;
  50. }
  51. RpcPath routePath = RpcPath.Parse(routePathString);
  52. if (!controllerRoutes.TryGetValue(routePath, out List<IRpcMethodProvider> methodProviders))
  53. {
  54. methodProviders = new List<IRpcMethodProvider>();
  55. controllerRoutes[routePath] = methodProviders;
  56. }
  57. methodProviders.Add(new ControllerPublicMethodProvider(controllerType.AsType()));
  58. }
  59. this.routeCache = controllerRoutes;
  60. }
  61. return this.routeCache;
  62. }
  63. /// <summary>
  64. /// Gets all the routes from all the controllers derived from the
  65. /// configured base controller type
  66. /// </summary>
  67. /// <returns>All the available routes</returns>
  68. public HashSet<RpcPath> GetRoutes()
  69. {
  70. Dictionary<RpcPath, List<IRpcMethodProvider>> routes = this.GetAllRoutes();
  71. return new HashSet<RpcPath>(routes.Keys);
  72. }
  73. /// <summary>
  74. /// Gets all the method providers for the specified path
  75. /// </summary>
  76. /// <param name="path">Path to the methods</param>
  77. /// <returns>All method providers for the specified path</returns>
  78. public List<IRpcMethodProvider> GetMethodsByPath(RpcPath path)
  79. {
  80. Dictionary<RpcPath, List<IRpcMethodProvider>> routes = this.GetAllRoutes();
  81. if (!routes.TryGetValue(path, out List<IRpcMethodProvider> methods))
  82. {
  83. return new List<IRpcMethodProvider>();
  84. }
  85. return methods;
  86. }
  87. }
  88. }
  89. #endif