mirror of https://github.com/abpframework/abp.git
14 changed files with 399 additions and 341 deletions
@ -0,0 +1,15 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.DynamicProxy |
|||
{ |
|||
public abstract class AbpInterceptor : IAbpInterceptor |
|||
{ |
|||
public abstract void Intercept(IAbpMethodInvocation invocation); |
|||
|
|||
public virtual Task InterceptAsync(IAbpMethodInvocation invocation) |
|||
{ |
|||
Intercept(invocation); |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
@ -1,181 +1,193 @@ |
|||
using System; |
|||
using System.Reflection; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Threading |
|||
{ |
|||
public static class InternalAsyncHelper //TODO: Rename since it's not internal anymore!
|
|||
{ |
|||
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) |
|||
.GetTypeInfo() |
|||
.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) |
|||
.GetTypeInfo() |
|||
.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) |
|||
.GetTypeInfo() |
|||
.GetMethod("AwaitTaskWithPreActionAndPostActionAndFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static) |
|||
.MakeGenericMethod(taskReturnType) |
|||
.Invoke(null, new object[] { actualReturnValue, preAction, postAction, finalAction }); |
|||
} |
|||
} |
|||
//TODO: Rename since it's not internal anymore!
|
|||
//TODO: Cache GetMethod reflection!
|
|||
public 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) |
|||
.GetTypeInfo() |
|||
.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) |
|||
.GetTypeInfo() |
|||
.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 |
|||
{ |
|||
finalAction?.Invoke(exception); |
|||
} |
|||
} |
|||
|
|||
public static object CallAwaitTaskWithPreActionAndPostActionAndFinallyAndGetResult(Type taskReturnType, Func<object> actualReturnValue, Func<Task> preAction = null, Func<Task> postAction = null, Action<Exception> finalAction = null) |
|||
{ |
|||
var returnFunc = typeof(InternalAsyncHelper) |
|||
.GetTypeInfo() |
|||
.GetMethod("ConvertFuncOfObjectToFuncOfTask", BindingFlags.NonPublic | BindingFlags.Static) |
|||
.MakeGenericMethod(taskReturnType) |
|||
.Invoke(null, new object[] {actualReturnValue}); |
|||
|
|||
return typeof(InternalAsyncHelper) |
|||
.GetTypeInfo() |
|||
.GetMethod("AwaitTaskWithPreActionAndPostActionAndFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static) |
|||
.MakeGenericMethod(taskReturnType) |
|||
.Invoke(null, new object[] { returnFunc, preAction, postAction, finalAction }); |
|||
} |
|||
|
|||
[UsedImplicitly] |
|||
private static Func<Task<T>> ConvertFuncOfObjectToFuncOfTask<T>(Func<object> actualReturnValue) |
|||
{ |
|||
return () => (Task<T>)actualReturnValue(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DynamicProxy; |
|||
using Volo.Abp.TestBase.Logging; |
|||
using Volo.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Castle.DynamicProxy |
|||
{ |
|||
public class SimpleAsyncInterceptor : AbpInterceptor, ITransientDependency |
|||
{ |
|||
public override void Intercept(IAbpMethodInvocation invocation) |
|||
{ |
|||
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add($"{GetType().Name}_Intercept_BeforeInvocation"); |
|||
invocation.ProceedAsync(); |
|||
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add($"{GetType().Name}_Intercept_AfterInvocation"); |
|||
} |
|||
|
|||
public override async Task InterceptAsync(IAbpMethodInvocation invocation) |
|||
{ |
|||
//await Task.Delay(5); CAN NOT USE await before method execution! This is a restriction of Castle DynamicProxy
|
|||
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add($"{GetType().Name}_InterceptAsync_BeforeInvocation"); |
|||
await invocation.ProceedAsync(); |
|||
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add($"{GetType().Name}_InterceptAsync_AfterInvocation"); |
|||
await Task.Delay(5); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.Castle.DynamicProxy |
|||
{ |
|||
public class SimpleAsyncInterceptor2 : SimpleAsyncInterceptor |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -1,31 +0,0 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DynamicProxy; |
|||
using Volo.Abp.TestBase.Logging; |
|||
using Volo.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Castle.DynamicProxy |
|||
{ |
|||
public class SimpleInterceptor : IAbpInterceptor, ITransientDependency |
|||
{ |
|||
public async Task InterceptAsync(IAbpMethodInvocation invocation) |
|||
{ |
|||
//await Task.Delay(5);
|
|||
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor_BeforeInvocation"); |
|||
await invocation.ProceedAsync(); |
|||
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor_AfterInvocation"); |
|||
await Task.Delay(5); |
|||
} |
|||
} |
|||
|
|||
public class SimpleInterceptor2 : IAbpInterceptor, ITransientDependency |
|||
{ |
|||
public async Task InterceptAsync(IAbpMethodInvocation invocation) |
|||
{ |
|||
//await Task.Delay(5);
|
|||
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor2_BeforeInvocation"); |
|||
await invocation.ProceedAsync(); |
|||
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor2_AfterInvocation"); |
|||
await Task.Delay(5); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
using Volo.Abp.DynamicProxy; |
|||
using Volo.Abp.TestBase.Logging; |
|||
using Volo.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Castle.DynamicProxy |
|||
{ |
|||
public class SimpleSyncInterceptor : AbpInterceptor, ITransientDependency |
|||
{ |
|||
public override void Intercept(IAbpMethodInvocation invocation) |
|||
{ |
|||
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add($"{GetType().Name}_Intercept_BeforeInvocation"); |
|||
invocation.Proceed(); |
|||
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add($"{GetType().Name}_Intercept_AfterInvocation"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue