LocalApiNode.cs 6.2 KB

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