ApiBind.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using VCommon.Json;
  10. using VCommon.Logging;
  11. using VCommon.VOpenApi.Json;
  12. namespace VCommon.VOpenApi
  13. {
  14. [DebuggerDisplay("ApiBind {RawRoute}")]
  15. public class ApiBind
  16. {
  17. private static readonly JsonReturnValueSerializer ReturnValueSerializer = new JsonReturnValueSerializer();
  18. private readonly Type _interfaceType;
  19. private readonly Func<object, object, object> _preCompiledInvoke;
  20. public string RawRoute { get; }
  21. public Type ApiResourceType { get; }
  22. public MethodInfo ApiMethod { get; }
  23. public Type InputParamType { get; }
  24. public string ResourceName { get; }
  25. public string Name { get; }
  26. public ApiBind(MethodInfo apiMethod, string resourceName, string name, Type interfaceType, string rawRoute)
  27. {
  28. _interfaceType = interfaceType;
  29. ApiMethod = apiMethod;
  30. ResourceName = resourceName;
  31. Name = name;
  32. ApiResourceType = apiMethod.DeclaringType;
  33. var parameters = ApiMethod.GetParameters();
  34. if (1 == parameters.Length)
  35. {
  36. InputParamType = parameters[0].ParameterType;
  37. }
  38. RawRoute = rawRoute;
  39. //Compile method invoke reduce time
  40. {
  41. var hasParam = InputParamType != null;
  42. var hasReturn = apiMethod.ReturnType != typeof(void);
  43. var pInstance = Expression.Parameter(typeof(object), "instance");
  44. var unboxInstance = Expression.Convert(pInstance, interfaceType);
  45. var pInput = Expression.Parameter(typeof(object), "input");
  46. MethodCallExpression invoke;
  47. if (hasParam)
  48. {
  49. var unboxInput = Expression.Convert(pInput, InputParamType);
  50. invoke = Expression.Call(unboxInstance, apiMethod, unboxInput);
  51. }
  52. else
  53. {
  54. invoke = Expression.Call(unboxInstance, apiMethod);
  55. }
  56. if (hasParam)
  57. {
  58. if (hasReturn)
  59. {
  60. var cvr = Expression.Convert(invoke, typeof(object));
  61. var lam = Expression.Lambda<Func<object, object, object>>(cvr, pInstance, pInput);
  62. _preCompiledInvoke = lam.Compile();
  63. }
  64. else
  65. {
  66. var lam = Expression.Lambda<Action<object, object>>(invoke, pInstance, pInput);
  67. var del = lam.Compile();
  68. _preCompiledInvoke = (instance, input) =>
  69. {
  70. del(instance, input);
  71. return null;
  72. };
  73. }
  74. }
  75. else
  76. {
  77. if (hasReturn)
  78. {
  79. var cvr = Expression.Convert(invoke, typeof(object));
  80. var lam = Expression.Lambda<Func<object, object>>(cvr, pInstance);
  81. var del = lam.Compile();
  82. _preCompiledInvoke = (instance, _) => del(instance);
  83. }
  84. else
  85. {
  86. var lam = Expression.Lambda<Action<object>>(invoke, pInstance);
  87. var del = lam.Compile();
  88. _preCompiledInvoke = (instance, _) =>
  89. {
  90. del(instance);
  91. return null;
  92. };
  93. }
  94. }
  95. }
  96. }
  97. public async Task Invoke(Func<Type, object> resolveFunc, Stream inputStream, Stream outputStream)
  98. {
  99. object inputParam = null;
  100. if (null != InputParamType)
  101. {
  102. //TODO: PACT? Scheme check?
  103. var inputReader = new StreamReader(inputStream, Encoding.UTF8);
  104. var input = await inputReader.ReadToEndAsync();
  105. try
  106. {
  107. inputParam = VJsonSerializer.Deserialize(input, InputParamType);
  108. }
  109. catch (JsonReaderException exception)
  110. {
  111. throw new VApiArgumentException("参数错误:" + exception.Message);
  112. }
  113. catch (JsonSerializationException exception)
  114. {
  115. throw new VApiArgumentException($"参数错误:{exception.Message}{exception.InnerException?.Message}");
  116. }
  117. catch (Exception exception)
  118. {
  119. Logger.Error("ApiBind: Error on parsing input", new { inputJson = input, exception });
  120. throw new VApiArgumentException("参数错误");
  121. }
  122. }
  123. var inst = resolveFunc(_interfaceType);
  124. var returnValue = _preCompiledInvoke(inst, inputParam);
  125. var output = ReturnValueSerializer.SerializeObject(returnValue);
  126. var bufOutput = Encoding.UTF8.GetBytes(output);
  127. await outputStream.WriteAsync(bufOutput, 0, bufOutput.Length);
  128. }
  129. }
  130. }