0
0

TransactionAsyncInterceptor.cs 4.5 KB

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