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; |
||||
using System.Reflection; |
using System.Reflection; |
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
namespace Volo.Abp.Threading |
namespace Volo.Abp.Threading |
||||
{ |
{ |
||||
public static class InternalAsyncHelper //TODO: Rename since it's not internal anymore!
|
//TODO: Rename since it's not internal anymore!
|
||||
{ |
//TODO: Cache GetMethod reflection!
|
||||
public static async Task AwaitTaskWithFinally(Task actualReturnValue, Action<Exception> finalAction) |
public static class InternalAsyncHelper |
||||
{ |
{ |
||||
Exception exception = null; |
public static async Task AwaitTaskWithFinally(Task actualReturnValue, Action<Exception> finalAction) |
||||
|
{ |
||||
try |
Exception exception = null; |
||||
{ |
|
||||
await actualReturnValue; |
try |
||||
} |
{ |
||||
catch (Exception ex) |
await actualReturnValue; |
||||
{ |
} |
||||
exception = ex; |
catch (Exception ex) |
||||
throw; |
{ |
||||
} |
exception = ex; |
||||
finally |
throw; |
||||
{ |
} |
||||
finalAction(exception); |
finally |
||||
} |
{ |
||||
} |
finalAction(exception); |
||||
|
} |
||||
public static async Task AwaitTaskWithPostActionAndFinally(Task actualReturnValue, Func<Task> postAction, Action<Exception> finalAction) |
} |
||||
{ |
|
||||
Exception exception = null; |
public static async Task AwaitTaskWithPostActionAndFinally(Task actualReturnValue, Func<Task> postAction, Action<Exception> finalAction) |
||||
|
{ |
||||
try |
Exception exception = null; |
||||
{ |
|
||||
await actualReturnValue; |
try |
||||
await postAction(); |
{ |
||||
} |
await actualReturnValue; |
||||
catch (Exception ex) |
await postAction(); |
||||
{ |
} |
||||
exception = ex; |
catch (Exception ex) |
||||
throw; |
{ |
||||
} |
exception = ex; |
||||
finally |
throw; |
||||
{ |
} |
||||
finalAction(exception); |
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; |
public static async Task AwaitTaskWithPreActionAndPostActionAndFinally(Func<Task> actualReturnValue, Func<Task> preAction = null, Func<Task> postAction = null, Action<Exception> finalAction = null) |
||||
|
{ |
||||
try |
Exception exception = null; |
||||
{ |
|
||||
if (preAction != null) |
try |
||||
{ |
{ |
||||
await preAction(); |
if (preAction != null) |
||||
} |
{ |
||||
|
await preAction(); |
||||
await actualReturnValue(); |
} |
||||
|
|
||||
if (postAction != null) |
await actualReturnValue(); |
||||
{ |
|
||||
await postAction(); |
if (postAction != null) |
||||
} |
{ |
||||
} |
await postAction(); |
||||
catch (Exception ex) |
} |
||||
{ |
} |
||||
exception = ex; |
catch (Exception ex) |
||||
throw; |
{ |
||||
} |
exception = ex; |
||||
finally |
throw; |
||||
{ |
} |
||||
if (finalAction != null) |
finally |
||||
{ |
{ |
||||
finalAction(exception); |
if (finalAction != null) |
||||
} |
{ |
||||
} |
finalAction(exception); |
||||
} |
} |
||||
|
} |
||||
public static async Task<T> AwaitTaskWithFinallyAndGetResult<T>(Task<T> actualReturnValue, Action<Exception> finalAction) |
} |
||||
{ |
|
||||
Exception exception = null; |
public static async Task<T> AwaitTaskWithFinallyAndGetResult<T>(Task<T> actualReturnValue, Action<Exception> finalAction) |
||||
|
{ |
||||
try |
Exception exception = null; |
||||
{ |
|
||||
return await actualReturnValue; |
try |
||||
} |
{ |
||||
catch (Exception ex) |
return await actualReturnValue; |
||||
{ |
} |
||||
exception = ex; |
catch (Exception ex) |
||||
throw; |
{ |
||||
} |
exception = ex; |
||||
finally |
throw; |
||||
{ |
} |
||||
finalAction(exception); |
finally |
||||
} |
{ |
||||
} |
finalAction(exception); |
||||
|
} |
||||
public static object CallAwaitTaskWithFinallyAndGetResult(Type taskReturnType, object actualReturnValue, Action<Exception> finalAction) |
} |
||||
{ |
|
||||
return typeof(InternalAsyncHelper) |
public static object CallAwaitTaskWithFinallyAndGetResult(Type taskReturnType, object actualReturnValue, Action<Exception> finalAction) |
||||
.GetTypeInfo() |
{ |
||||
.GetMethod("AwaitTaskWithFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static) |
return typeof(InternalAsyncHelper) |
||||
.MakeGenericMethod(taskReturnType) |
.GetTypeInfo() |
||||
.Invoke(null, new object[] { actualReturnValue, finalAction }); |
.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; |
public static async Task<T> AwaitTaskWithPostActionAndFinallyAndGetResult<T>(Task<T> actualReturnValue, Func<Task> postAction, Action<Exception> finalAction) |
||||
|
{ |
||||
try |
Exception exception = null; |
||||
{ |
|
||||
var result = await actualReturnValue; |
try |
||||
await postAction(); |
{ |
||||
return result; |
var result = await actualReturnValue; |
||||
} |
await postAction(); |
||||
catch (Exception ex) |
return result; |
||||
{ |
} |
||||
exception = ex; |
catch (Exception ex) |
||||
throw; |
{ |
||||
} |
exception = ex; |
||||
finally |
throw; |
||||
{ |
} |
||||
finalAction(exception); |
finally |
||||
} |
{ |
||||
} |
finalAction(exception); |
||||
|
} |
||||
public static object CallAwaitTaskWithPostActionAndFinallyAndGetResult(Type taskReturnType, object actualReturnValue, Func<Task> action, Action<Exception> finalAction) |
} |
||||
{ |
|
||||
return typeof (InternalAsyncHelper) |
public static object CallAwaitTaskWithPostActionAndFinallyAndGetResult(Type taskReturnType, object actualReturnValue, Func<Task> action, Action<Exception> finalAction) |
||||
.GetTypeInfo() |
{ |
||||
.GetMethod("AwaitTaskWithPostActionAndFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static) |
return typeof(InternalAsyncHelper) |
||||
.MakeGenericMethod(taskReturnType) |
.GetTypeInfo() |
||||
.Invoke(null, new object[] { actualReturnValue, action, finalAction }); |
.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; |
public static async Task<T> AwaitTaskWithPreActionAndPostActionAndFinallyAndGetResult<T>(Func<Task<T>> actualReturnValue, Func<Task> preAction = null, Func<Task> postAction = null, Action<Exception> finalAction = null) |
||||
|
{ |
||||
try |
Exception exception = null; |
||||
{ |
|
||||
if (preAction != null) |
try |
||||
{ |
{ |
||||
await preAction(); |
if (preAction != null) |
||||
} |
{ |
||||
|
await preAction(); |
||||
var result = await actualReturnValue(); |
} |
||||
|
|
||||
if (postAction != null) |
var result = await actualReturnValue(); |
||||
{ |
|
||||
await postAction(); |
if (postAction != null) |
||||
} |
{ |
||||
|
await postAction(); |
||||
return result; |
} |
||||
} |
|
||||
catch (Exception ex) |
return result; |
||||
{ |
} |
||||
exception = ex; |
catch (Exception ex) |
||||
throw; |
{ |
||||
} |
exception = ex; |
||||
finally |
throw; |
||||
{ |
} |
||||
if (finalAction != null) |
finally |
||||
{ |
{ |
||||
finalAction(exception); |
finalAction?.Invoke(exception); |
||||
} |
} |
||||
} |
} |
||||
} |
|
||||
|
public static object CallAwaitTaskWithPreActionAndPostActionAndFinallyAndGetResult(Type taskReturnType, Func<object> actualReturnValue, Func<Task> preAction = null, Func<Task> postAction = null, Action<Exception> finalAction = null) |
||||
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) |
||||
return typeof(InternalAsyncHelper) |
.GetTypeInfo() |
||||
.GetTypeInfo() |
.GetMethod("ConvertFuncOfObjectToFuncOfTask", BindingFlags.NonPublic | BindingFlags.Static) |
||||
.GetMethod("AwaitTaskWithPreActionAndPostActionAndFinallyAndGetResult", BindingFlags.Public | BindingFlags.Static) |
.MakeGenericMethod(taskReturnType) |
||||
.MakeGenericMethod(taskReturnType) |
.Invoke(null, new object[] {actualReturnValue}); |
||||
.Invoke(null, new object[] { actualReturnValue, preAction, postAction, finalAction }); |
|
||||
} |
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