InternalAsyncHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Reflection;
  3. using System.Threading.Tasks;
  4. namespace Abp.Threading
  5. {
  6. internal static class InternalAsyncHelper
  7. {
  8. public static async Task AwaitTaskWithFinally(Task actualReturnValue, Action<Exception> finalAction)
  9. {
  10. Exception exception = null;
  11. try
  12. {
  13. await actualReturnValue;
  14. }
  15. catch (Exception ex)
  16. {
  17. exception = ex;
  18. throw;
  19. }
  20. finally
  21. {
  22. finalAction(exception);
  23. }
  24. }
  25. public static async Task AwaitTaskWithPostActionAndFinally(Task actualReturnValue, Func<Task> postAction, Action<Exception> finalAction)
  26. {
  27. Exception exception = null;
  28. try
  29. {
  30. await actualReturnValue;
  31. await postAction();
  32. }
  33. catch (Exception ex)
  34. {
  35. exception = ex;
  36. throw;
  37. }
  38. finally
  39. {
  40. finalAction(exception);
  41. }
  42. }
  43. public static async Task AwaitTaskWithPreActionAndPostActionAndFinally(Func<Task> actualReturnValue, Func<Task> preAction = null, Func<Task> postAction = null, Action<Exception> finalAction = null)
  44. {
  45. Exception exception = null;
  46. try
  47. {
  48. if (preAction != null)
  49. {
  50. await preAction();
  51. }
  52. await actualReturnValue();
  53. if (postAction != null)
  54. {
  55. await postAction();
  56. }
  57. }
  58. catch (Exception ex)
  59. {
  60. exception = ex;
  61. throw;
  62. }
  63. finally
  64. {
  65. if (finalAction != null)
  66. {
  67. finalAction(exception);
  68. }
  69. }
  70. }
  71. public static async Task<T> AwaitTaskWithFinallyAndGetResult<T>(Task<T> actualReturnValue, Action<Exception> finalAction)
  72. {
  73. Exception exception = null;
  74. try
  75. {
  76. return await actualReturnValue;
  77. }
  78. catch (Exception ex)
  79. {
  80. exception = ex;
  81. throw;
  82. }
  83. finally
  84. {
  85. finalAction(exception);
  86. }
  87. }
  88. public static object CallAwaitTaskWithFinallyAndGetResult(Type taskReturnType, object actualReturnValue, Action<Exception> finalAction)
  89. {
  90. return typeof(InternalAsyncHelper)
  91. .GetMethod("AwaitTaskWithFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static)
  92. .MakeGenericMethod(taskReturnType)
  93. .Invoke(null, new object[] { actualReturnValue, finalAction });
  94. }
  95. public static async Task<T> AwaitTaskWithPostActionAndFinallyAndGetResult<T>(Task<T> actualReturnValue, Func<Task> postAction, Action<Exception> finalAction)
  96. {
  97. Exception exception = null;
  98. try
  99. {
  100. var result = await actualReturnValue;
  101. await postAction();
  102. return result;
  103. }
  104. catch (Exception ex)
  105. {
  106. exception = ex;
  107. throw;
  108. }
  109. finally
  110. {
  111. finalAction(exception);
  112. }
  113. }
  114. public static object CallAwaitTaskWithPostActionAndFinallyAndGetResult(Type taskReturnType, object actualReturnValue, Func<Task> action, Action<Exception> finalAction)
  115. {
  116. return typeof (InternalAsyncHelper)
  117. .GetMethod("AwaitTaskWithPostActionAndFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static)
  118. .MakeGenericMethod(taskReturnType)
  119. .Invoke(null, new object[] { actualReturnValue, action, finalAction });
  120. }
  121. public static async Task<T> AwaitTaskWithPreActionAndPostActionAndFinallyAndGetResult<T>(Func<Task<T>> actualReturnValue, Func<Task> preAction = null, Func<Task> postAction = null, Action<Exception> finalAction = null)
  122. {
  123. Exception exception = null;
  124. try
  125. {
  126. if (preAction != null)
  127. {
  128. await preAction();
  129. }
  130. var result = await actualReturnValue();
  131. if (postAction != null)
  132. {
  133. await postAction();
  134. }
  135. return result;
  136. }
  137. catch (Exception ex)
  138. {
  139. exception = ex;
  140. throw;
  141. }
  142. finally
  143. {
  144. if (finalAction != null)
  145. {
  146. finalAction(exception);
  147. }
  148. }
  149. }
  150. public static object CallAwaitTaskWithPreActionAndPostActionAndFinallyAndGetResult(Type taskReturnType, Func<object> actualReturnValue, Func<Task> preAction = null, Func<Task> postAction = null, Action<Exception> finalAction = null)
  151. {
  152. return typeof(InternalAsyncHelper)
  153. .GetMethod("AwaitTaskWithPreActionAndPostActionAndFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static)
  154. .MakeGenericMethod(taskReturnType)
  155. .Invoke(null, new object[] { actualReturnValue, preAction, postAction, finalAction });
  156. }
  157. }
  158. }