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