| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using System;
- using System.Reflection;
- using System.Threading.Tasks;
- namespace Abp.Threading
- {
- internal static class InternalAsyncHelper
- {
- public static async Task AwaitTaskWithFinally(Task actualReturnValue, Action<Exception> finalAction)
- {
- Exception exception = null;
- try
- {
- await actualReturnValue;
- }
- catch (Exception ex)
- {
- exception = ex;
- throw;
- }
- finally
- {
- finalAction(exception);
- }
- }
- public static async Task AwaitTaskWithPostActionAndFinally(Task actualReturnValue, Func<Task> postAction, Action<Exception> finalAction)
- {
- Exception exception = null;
- try
- {
- await actualReturnValue;
- await postAction();
- }
- catch (Exception ex)
- {
- exception = ex;
- throw;
- }
- finally
- {
- finalAction(exception);
- }
- }
- public static async Task AwaitTaskWithPreActionAndPostActionAndFinally(Func<Task> actualReturnValue, Func<Task> preAction = null, Func<Task> postAction = null, Action<Exception> finalAction = null)
- {
- Exception exception = null;
- try
- {
- if (preAction != null)
- {
- await preAction();
- }
- await actualReturnValue();
- if (postAction != null)
- {
- await postAction();
- }
- }
- catch (Exception ex)
- {
- exception = ex;
- throw;
- }
- finally
- {
- if (finalAction != null)
- {
- finalAction(exception);
- }
- }
- }
- public static async Task<T> AwaitTaskWithFinallyAndGetResult<T>(Task<T> actualReturnValue, Action<Exception> finalAction)
- {
- Exception exception = null;
- try
- {
- return await actualReturnValue;
- }
- catch (Exception ex)
- {
- exception = ex;
- throw;
- }
- finally
- {
- finalAction(exception);
- }
- }
- public static object CallAwaitTaskWithFinallyAndGetResult(Type taskReturnType, object actualReturnValue, Action<Exception> finalAction)
- {
- return typeof(InternalAsyncHelper)
- .GetMethod("AwaitTaskWithFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static)
- .MakeGenericMethod(taskReturnType)
- .Invoke(null, new object[] { actualReturnValue, finalAction });
- }
- public static async Task<T> AwaitTaskWithPostActionAndFinallyAndGetResult<T>(Task<T> actualReturnValue, Func<Task> postAction, Action<Exception> finalAction)
- {
- Exception exception = null;
- try
- {
- var result = await actualReturnValue;
- await postAction();
- return result;
- }
- catch (Exception ex)
- {
- exception = ex;
- throw;
- }
- finally
- {
- finalAction(exception);
- }
- }
- public static object CallAwaitTaskWithPostActionAndFinallyAndGetResult(Type taskReturnType, object actualReturnValue, Func<Task> action, Action<Exception> finalAction)
- {
- return typeof (InternalAsyncHelper)
- .GetMethod("AwaitTaskWithPostActionAndFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static)
- .MakeGenericMethod(taskReturnType)
- .Invoke(null, new object[] { actualReturnValue, action, finalAction });
- }
- public static async Task<T> AwaitTaskWithPreActionAndPostActionAndFinallyAndGetResult<T>(Func<Task<T>> actualReturnValue, Func<Task> preAction = null, Func<Task> postAction = null, Action<Exception> finalAction = null)
- {
- Exception exception = null;
- try
- {
- if (preAction != null)
- {
- await preAction();
- }
- var result = await actualReturnValue();
- if (postAction != null)
- {
- await postAction();
- }
- return result;
- }
- catch (Exception ex)
- {
- exception = ex;
- throw;
- }
- finally
- {
- if (finalAction != null)
- {
- finalAction(exception);
- }
- }
- }
- public static object CallAwaitTaskWithPreActionAndPostActionAndFinallyAndGetResult(Type taskReturnType, Func<object> actualReturnValue, Func<Task> preAction = null, Func<Task> postAction = null, Action<Exception> finalAction = null)
- {
- return typeof(InternalAsyncHelper)
- .GetMethod("AwaitTaskWithPreActionAndPostActionAndFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static)
- .MakeGenericMethod(taskReturnType)
- .Invoke(null, new object[] { actualReturnValue, preAction, postAction, finalAction });
- }
- }
- }
|