ApiBind.cs 5.3 KB

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