From 370d0e3b1c890ab681b0c21cd021f48f5cda846d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 6 May 2017 14:10:43 +0300 Subject: [PATCH] Implemented async interception. --- .../CastleAbpInterceptorAdapter.cs | 19 ++++++++++++++++++- .../CastleAbpMethodInvocationAdapter.cs | 14 ++++++++++++-- .../Interception/Autofac_Interception_Test.cs | 4 ++-- .../CastleInterceptionTestBase.cs | 5 +++-- .../Castle/DynamicProxy/SimpleInterceptor.cs | 14 +++++++++++--- 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpInterceptorAdapter.cs b/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpInterceptorAdapter.cs index adf674ac7e..bae546e095 100644 --- a/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpInterceptorAdapter.cs +++ b/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpInterceptorAdapter.cs @@ -17,7 +17,19 @@ namespace Volo.Abp.Castle.DynamicProxy public void Intercept(IInvocation invocation) { - if (invocation.MethodInvocationTarget.IsAsync() && _abpInterceptor is IAbpAsyncInterceptor) + if (invocation.MethodInvocationTarget.IsAsync()) + { + InterceptAsyncMethod(invocation); + } + else + { + InterceptSyncMethod(invocation); + } + } + + private void InterceptAsyncMethod(IInvocation invocation) + { + if (_abpInterceptor is IAbpAsyncInterceptor) { _abpInterceptor.As().InterceptAsync(new CastleAbpAsyncMethodInvocationAdapter(invocation)); } @@ -26,5 +38,10 @@ namespace Volo.Abp.Castle.DynamicProxy _abpInterceptor.Intercept(new CastleAbpMethodInvocationAdapter(invocation)); } } + + private void InterceptSyncMethod(IInvocation invocation) + { + _abpInterceptor.Intercept(new CastleAbpMethodInvocationAdapter(invocation)); + } } } diff --git a/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpMethodInvocationAdapter.cs b/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpMethodInvocationAdapter.cs index 4a2dcf5cfe..c15aa48d8f 100644 --- a/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpMethodInvocationAdapter.cs +++ b/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpMethodInvocationAdapter.cs @@ -1,5 +1,7 @@ -using Castle.DynamicProxy; +using System.Threading.Tasks; +using Castle.DynamicProxy; using Volo.Abp.DynamicProxy; +using Volo.Abp.Threading; namespace Volo.Abp.Castle.DynamicProxy { @@ -13,7 +15,15 @@ namespace Volo.Abp.Castle.DynamicProxy public void Proceed() { - Invocation.Proceed(); + if (Invocation.Method.IsAsync()) + { + Invocation.Proceed(); + AsyncHelper.RunSync(() => (Task) Invocation.ReturnValue); + } + else + { + Invocation.Proceed(); + } } } } \ No newline at end of file diff --git a/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/Interception/Autofac_Interception_Test.cs b/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/Interception/Autofac_Interception_Test.cs index d24aedf04b..83d5a50dda 100644 --- a/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/Interception/Autofac_Interception_Test.cs +++ b/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/Interception/Autofac_Interception_Test.cs @@ -34,11 +34,11 @@ namespace Volo.Abp.Autofac.Interception result.ShouldBe(42); target.Logs.Count.ShouldBe(5); - target.Logs[0].ShouldBe("BeforeInvocation"); + target.Logs[0].ShouldBe("SimpleInterceptor_BeforeInvocation"); target.Logs[1].ShouldBe("EnterGetValueAsync"); target.Logs[2].ShouldBe("MiddleGetValueAsync"); target.Logs[3].ShouldBe("ExitGetValueAsync"); - target.Logs[4].ShouldBe("AfterInvocation"); + target.Logs[4].ShouldBe("SimpleInterceptor_AfterInvocation"); } } } diff --git a/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/CastleInterceptionTestBase.cs b/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/CastleInterceptionTestBase.cs index f8ad33481f..497d1a3fc0 100644 --- a/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/CastleInterceptionTestBase.cs +++ b/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/CastleInterceptionTestBase.cs @@ -7,6 +7,7 @@ using Xunit; namespace Volo.Abp.Castle.DynamicProxy { + //TODO: There is no Castle Dependency here.. We can move this base class directly to Volo.Abp.Tests public abstract class CastleInterceptionTestBase : AbpIntegratedTest where TStartupModule : IAbpModule { @@ -25,9 +26,9 @@ namespace Volo.Abp.Castle.DynamicProxy result.ShouldBe(42); target.Logs.Count.ShouldBe(3); - target.Logs[0].ShouldBe("BeforeInvocation"); + target.Logs[0].ShouldBe("SimpleInterceptor_BeforeInvocation"); target.Logs[1].ShouldBe("ExecutingGetValue"); - target.Logs[2].ShouldBe("AfterInvocation"); + target.Logs[2].ShouldBe("SimpleInterceptor_AfterInvocation"); } } } \ No newline at end of file diff --git a/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/SimpleInterceptor.cs b/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/SimpleInterceptor.cs index c7ac128415..e900d9a667 100644 --- a/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/SimpleInterceptor.cs +++ b/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/SimpleInterceptor.cs @@ -1,16 +1,24 @@ +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 class SimpleInterceptor : IAbpInterceptor, /*IAbpAsyncInterceptor,*/ ITransientDependency { public void Intercept(IAbpMethodInvocation invocation) { - (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("BeforeInvocation"); + (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor_BeforeInvocation"); invocation.Proceed(); - (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("AfterInvocation"); + (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor_AfterInvocation"); } + + //public async Task InterceptAsync(IAbpAsyncMethodInvocation invocation) + //{ + // (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor_BeforeInvocation"); + // await invocation.ProceedAsync(); + // (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor_AfterInvocation"); + //} } } \ No newline at end of file