TransactionAsyncInterceptor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using Admin.Core.Common.Attributes;
  2. using Admin.Core.Common.Output;
  3. using Admin.Core.Repository;
  4. using Castle.DynamicProxy;
  5. using FreeSql;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace Admin.Core.Aop
  9. {
  10. public class TransactionAsyncInterceptor : IAsyncInterceptor
  11. {
  12. private IUnitOfWork _unitOfWork;
  13. private readonly MyUnitOfWorkManager _unitOfWorkManager;
  14. public TransactionAsyncInterceptor(MyUnitOfWorkManager unitOfWorkManager)
  15. {
  16. _unitOfWorkManager = unitOfWorkManager;
  17. }
  18. private bool TryBegin(IInvocation invocation)
  19. {
  20. var method = invocation.MethodInvocationTarget ?? invocation.Method;
  21. var attribute = method.GetCustomAttributes(typeof(TransactionAttribute), false).FirstOrDefault();
  22. if (attribute is TransactionAttribute transaction)
  23. {
  24. _unitOfWork = _unitOfWorkManager.Begin(transaction.Propagation, transaction.IsolationLevel);
  25. return true;
  26. }
  27. return false;
  28. }
  29. private async Task InternalInterceptAsynchronous(IInvocation invocation)
  30. {
  31. //string methodName =
  32. // $"{invocation.MethodInvocationTarget.DeclaringType?.FullName}.{invocation.Method.Name}()";
  33. //int? hashCode = _unitOfWork.GetHashCode();
  34. invocation.Proceed();
  35. try
  36. {
  37. //处理Task返回一个null值的情况会导致空指针
  38. if (invocation.ReturnValue != null)
  39. {
  40. await (Task)invocation.ReturnValue;
  41. }
  42. _unitOfWork.Commit();
  43. }
  44. catch (System.Exception)
  45. {
  46. _unitOfWork.Rollback();
  47. throw;
  48. }
  49. finally
  50. {
  51. _unitOfWork.Dispose();
  52. }
  53. }
  54. private async Task<TResult> InternalInterceptAsynchronous<TResult>(IInvocation invocation)
  55. {
  56. TResult result;
  57. if (TryBegin(invocation))
  58. {
  59. try
  60. {
  61. invocation.Proceed();
  62. result = await (Task<TResult>)invocation.ReturnValue;
  63. if (result is IResponseOutput res && !res.Success)
  64. {
  65. _unitOfWork.Rollback();
  66. }
  67. else
  68. {
  69. _unitOfWork.Commit();
  70. }
  71. }
  72. catch (System.Exception)
  73. {
  74. _unitOfWork.Rollback();
  75. throw;
  76. }
  77. finally
  78. {
  79. _unitOfWork.Dispose();
  80. }
  81. }
  82. else
  83. {
  84. invocation.Proceed();
  85. result = await (Task<TResult>)invocation.ReturnValue;
  86. }
  87. return result;
  88. }
  89. /// <summary>
  90. /// 拦截同步执行的方法
  91. /// </summary>
  92. /// <param name="invocation"></param>
  93. public void InterceptSynchronous(IInvocation invocation)
  94. {
  95. if (TryBegin(invocation))
  96. {
  97. try
  98. {
  99. invocation.Proceed();
  100. var result = invocation.ReturnValue;
  101. if (result is IResponseOutput res && !res.Success)
  102. {
  103. _unitOfWork.Rollback();
  104. }
  105. else
  106. {
  107. _unitOfWork.Commit();
  108. }
  109. }
  110. catch
  111. {
  112. _unitOfWork.Rollback();
  113. throw;
  114. }
  115. finally
  116. {
  117. _unitOfWork.Dispose();
  118. }
  119. }
  120. else
  121. {
  122. invocation.Proceed();
  123. }
  124. }
  125. /// <summary>
  126. /// 拦截返回结果为Task的方法
  127. /// </summary>
  128. /// <param name="invocation"></param>
  129. public void InterceptAsynchronous(IInvocation invocation)
  130. {
  131. if (TryBegin(invocation))
  132. {
  133. invocation.ReturnValue = InternalInterceptAsynchronous(invocation);
  134. }
  135. else
  136. {
  137. invocation.Proceed();
  138. }
  139. }
  140. /// <summary>
  141. /// 拦截返回结果为Task<TResult>的方法
  142. /// </summary>
  143. /// <typeparam name="TResult"></typeparam>
  144. /// <param name="invocation"></param>
  145. public void InterceptAsynchronous<TResult>(IInvocation invocation)
  146. {
  147. invocation.ReturnValue = InternalInterceptAsynchronous<TResult>(invocation);
  148. }
  149. }
  150. }