LocalApiNode.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Newtonsoft.Json;
  7. using Sers.Core.Module.Api.ApiDesc;
  8. using Sers.Core.Module.Api.LocalApi;
  9. using Sers.Core.Module.Rpc;
  10. using Vit.Core.Util.Dynamic;
  11. using Vit.Extensions;
  12. using Vit.Ioc;
  13. namespace Sers.NetcoreLoader
  14. {
  15. [JsonObject(MemberSerialization.OptIn)]
  16. public class LocalApiNode: IApiNode
  17. {
  18. [JsonProperty]
  19. public SsApiDesc apiDesc { get; set; }
  20. DynamicMethodExecutor executor;
  21. MethodInfo apiController_Method;
  22. Func<ArraySegment<byte>, object[]> Arg_Deserialize;
  23. Func<object, byte[]> ReturnValue_SerializeToBytes = ObjectSerializeExtensions.SerializeToBytes;
  24. public LocalApiNode(SsApiDesc apiDesc, MethodInfo apiController_Method)
  25. {
  26. this.apiDesc = apiDesc;
  27. this.apiController_Method = apiController_Method;
  28. executor = new DynamicMethodExecutor(apiController_Method);
  29. var OnDeserialize=Arg_Deserialize = apiDesc.argType.OnDeserialize;
  30. #region returnType is ActionResult 、Task
  31. {
  32. var returnType = apiController_Method.ReturnType;
  33. //(x.x.1)ActionResult
  34. if (returnType.IsGenericType && typeof(ActionResult<>).IsAssignableFrom(returnType.GetGenericTypeDefinition()))
  35. {
  36. var propertyInfo = returnType.GetProperty("Value");
  37. ReturnValue_SerializeToBytes = (obj) => {
  38. return propertyInfo.GetValue(obj).SerializeToBytes();
  39. };
  40. }
  41. //(x.x.2)Task
  42. if (returnType.IsGenericType && typeof(Task<>).IsAssignableFrom(returnType.GetGenericTypeDefinition()))
  43. {
  44. var propertyInfo = returnType.GetProperty("Result");
  45. //是否为 ActionResult
  46. var taskResultType = returnType.GetGenericArguments()[0];
  47. if (taskResultType.IsGenericType && typeof(ActionResult<>).IsAssignableFrom(taskResultType.GetGenericTypeDefinition()))
  48. {
  49. var propertyInfo_ActionResult = taskResultType.GetProperty("Value");
  50. ReturnValue_SerializeToBytes = (obj) =>
  51. {
  52. (obj as Task)?.Wait();
  53. var taskResult = propertyInfo.GetValue(obj);
  54. var actionResult = propertyInfo_ActionResult.GetValue(taskResult);
  55. return actionResult?.SerializeToBytes();
  56. };
  57. }
  58. else
  59. {
  60. ReturnValue_SerializeToBytes = (obj) => {
  61. (obj as Task)?.Wait();
  62. var taskResult = propertyInfo.GetValue(obj);
  63. return taskResult?.SerializeToBytes();
  64. };
  65. }
  66. }
  67. }
  68. #endregion
  69. #region 转换 特殊 route "/a/b/{id}/{name}" 为 "/a/b/*"
  70. ArgFromUrl();
  71. void ArgFromUrl()
  72. {
  73. var oriRoute = apiDesc.OriRouteGet();
  74. int index = oriRoute.IndexOf("/{");
  75. if (index < 0) return;
  76. #region url
  77. var routeArgNames = oriRoute.Substring(index+1).Replace("{", "").Replace("}", "");
  78. var urlArgNames = routeArgNames.Split('/');
  79. if (urlArgNames == null || urlArgNames.Length == 0) return;
  80. #endregion
  81. //arg
  82. ParameterInfo[] infos = apiController_Method.GetParameters();
  83. if (infos == null || infos.Length == 0) return;
  84. #region build arg map
  85. //(int urlIndex, int argIndex, Type argType)
  86. var maps = new List<(int, int, Type)>();
  87. for (int i = 0; i < infos.Length; i++)
  88. {
  89. var info = infos[i];
  90. for (int j = 0; j < urlArgNames.Length; j++)
  91. {
  92. if (info.Name == urlArgNames[j])
  93. {
  94. maps.Add((j, i, info.ParameterType));
  95. break;
  96. }
  97. }
  98. }
  99. if (maps.Count == 0) return;
  100. #endregion
  101. #region Arg_Deserialize
  102. Arg_Deserialize = (arg_OriData) =>
  103. {
  104. var args = OnDeserialize(arg_OriData);
  105. var argsFromUrl = GetArgsFromUrl();
  106. foreach ((int urlIndex, int argIndex, Type argType) in maps)
  107. {
  108. if (argsFromUrl.Length > urlIndex)
  109. {
  110. args[argIndex] = argsFromUrl[urlIndex].Deserialize(argType);
  111. }
  112. }
  113. return args;
  114. };
  115. #endregion
  116. }
  117. string[] GetArgsFromUrl()
  118. {
  119. return RpcContext.RpcData.http_url_RelativeUrl_Get()?.Split('/');
  120. }
  121. #endregion
  122. }
  123. public /*virtual*/ byte[] Invoke(ArraySegment<byte> arg_OriData)
  124. {
  125. //(x.1)反序列化 请求参数
  126. //var args = apiDesc.argType?.Deserialize(arg_OriData);
  127. var args = Arg_Deserialize(arg_OriData);
  128. //(x.2) Invoke
  129. //var returnValue = apiController_Method.Invoke(IocHelp.Create(apiController_Method.DeclaringType), args);
  130. var returnValue = executor.Execute(IocHelp.Create(apiController_Method.DeclaringType), args);
  131. //(x.3) 序列化 返回数据
  132. return ReturnValue_SerializeToBytes(returnValue);
  133. }
  134. }
  135. }