From c6ffb58841c37bdda9870dd3fd5e7ce2a240d7fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 6 May 2017 22:46:15 +0300 Subject: [PATCH] Refactored and imporved interception abstraction. --- .../AbpRegistrationBuilderExtensions.cs | 1 + .../CastleAbpAsyncMethodInvocationAdapter.cs | 21 ----- .../CastleAbpInterceptorAdapter.cs | 18 +++-- .../CastleAbpMethodInvocationAdapter.cs | 50 ++++++++++-- .../CastleAbpMethodInvocationAdapterBase.cs | 31 -------- .../Abp/DynamicProxy/IAbpAsyncInterceptor.cs | 9 --- .../DynamicProxy/IAbpAsyncMethodInvocation.cs | 9 --- .../Volo/Abp/DynamicProxy/IAbpInterceptor.cs | 6 +- .../Abp/DynamicProxy/IAbpMethodInvocation.cs | 18 ++++- .../DynamicProxy/IAbpMethodInvocationCore.cs | 18 ----- .../Volo/Abp/Uow/UnitOfWorkInterceptor.cs | 50 +----------- .../Interception/Autofac_Interception_Test.cs | 76 ++++++++++++++++++- .../Abp/Castle/AbpCastleCoreTestModule.cs | 1 + .../CastleInterceptionTestBase.cs | 18 ----- .../SimpleInterceptionTargetClass.cs | 18 ++++- .../Castle/DynamicProxy/SimpleInterceptor.cs | 25 +++--- 16 files changed, 183 insertions(+), 186 deletions(-) delete mode 100644 src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpAsyncMethodInvocationAdapter.cs delete mode 100644 src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpMethodInvocationAdapterBase.cs delete mode 100644 src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpAsyncInterceptor.cs delete mode 100644 src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpAsyncMethodInvocation.cs delete mode 100644 src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpMethodInvocationCore.cs diff --git a/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index 69379461d9..e26f6b4070 100644 --- a/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -54,6 +54,7 @@ namespace Autofac.Builder typeof(CastleAbpInterceptorAdapter<>).MakeGenericType(interceptor) ); } + return registrationBuilder; } } diff --git a/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpAsyncMethodInvocationAdapter.cs b/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpAsyncMethodInvocationAdapter.cs deleted file mode 100644 index fca81bc053..0000000000 --- a/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpAsyncMethodInvocationAdapter.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Threading.Tasks; -using Castle.DynamicProxy; -using Volo.Abp.DynamicProxy; - -namespace Volo.Abp.Castle.DynamicProxy -{ - public class CastleAbpAsyncMethodInvocationAdapter : CastleAbpMethodInvocationAdapterBase, IAbpAsyncMethodInvocation - { - public CastleAbpAsyncMethodInvocationAdapter(IInvocation invocation) - : base(invocation) - { - - } - - public Task ProceedAsync() - { - Invocation.Proceed(); - return (Task)Invocation.ReturnValue; - } - } -} \ No newline at end of file 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 bae546e095..6f60cb1830 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 @@ -1,4 +1,5 @@ -using Castle.DynamicProxy; +using System.Threading.Tasks; +using Castle.DynamicProxy; using Volo.Abp.DynamicProxy; using Volo.Abp.Threading; using Volo.ExtensionMethods; @@ -29,19 +30,26 @@ namespace Volo.Abp.Castle.DynamicProxy private void InterceptAsyncMethod(IInvocation invocation) { - if (_abpInterceptor is IAbpAsyncInterceptor) + if (invocation.Method.ReturnType == typeof(Task)) { - _abpInterceptor.As().InterceptAsync(new CastleAbpAsyncMethodInvocationAdapter(invocation)); + invocation.ReturnValue = _abpInterceptor.InterceptAsync(new CastleAbpMethodInvocationAdapter(invocation)); } else { - _abpInterceptor.Intercept(new CastleAbpMethodInvocationAdapter(invocation)); + var interceptResult = _abpInterceptor.InterceptAsync(new CastleAbpMethodInvocationAdapter(invocation)); + + invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( + invocation.Method.ReturnType.GenericTypeArguments[0], + invocation.ReturnValue, //TODO: Can not change return value in that case !!! Create an interceptor that changes return value, for test purposes! + () => interceptResult, + exception => { } + ); } } private void InterceptSyncMethod(IInvocation invocation) { - _abpInterceptor.Intercept(new CastleAbpMethodInvocationAdapter(invocation)); + AsyncHelper.RunSync(() => _abpInterceptor.InterceptAsync(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 c15aa48d8f..acb2963ce8 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,28 +1,62 @@ -using System.Threading.Tasks; +using System; +using System.Reflection; +using System.Threading.Tasks; using Castle.DynamicProxy; using Volo.Abp.DynamicProxy; using Volo.Abp.Threading; namespace Volo.Abp.Castle.DynamicProxy { - public class CastleAbpMethodInvocationAdapter : CastleAbpMethodInvocationAdapterBase, IAbpMethodInvocation + public class CastleAbpMethodInvocationAdapter : IAbpMethodInvocation { + public object[] Arguments => Invocation.Arguments; + + public Type[] GenericArguments => Invocation.GenericArguments; + + public object TargetObject => Invocation.InvocationTarget; + + public MethodInfo Method => Invocation.MethodInvocationTarget; + + public object ReturnValue + { + get => Invocation.ReturnValue; + set => Invocation.ReturnValue = value; + } + + protected IInvocation Invocation { get; } + public CastleAbpMethodInvocationAdapter(IInvocation invocation) - : base(invocation) { + Invocation = invocation; + } + public Task ProceedAsync() + { + return Invocation.Method.IsAsync() + ? RunAsync() + : RunSync(); } - public void Proceed() + private Task RunAsync() { - if (Invocation.Method.IsAsync()) + Invocation.Proceed(); + return (Task) Invocation.ReturnValue; + } + + private Task RunSync() + { + Invocation.Proceed(); + + if (Method.ReturnType == typeof(void)) { - Invocation.Proceed(); - AsyncHelper.RunSync(() => (Task) Invocation.ReturnValue); + return Task.CompletedTask; } else { - Invocation.Proceed(); + return (Task) typeof(Task) + .GetMethod("FromResult", BindingFlags.Static | BindingFlags.Public) + .MakeGenericMethod(Method.ReturnType) + .Invoke(null, new object[] {Invocation.ReturnValue}); } } } diff --git a/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpMethodInvocationAdapterBase.cs b/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpMethodInvocationAdapterBase.cs deleted file mode 100644 index 788960b206..0000000000 --- a/src/Volo.Abp.Castle.Core/Volo/Abp/Castle/DynamicProxy/CastleAbpMethodInvocationAdapterBase.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Reflection; -using Castle.DynamicProxy; -using Volo.Abp.DynamicProxy; - -namespace Volo.Abp.Castle.DynamicProxy -{ - public abstract class CastleAbpMethodInvocationAdapterBase : IAbpMethodInvocationCore - { - public object[] Arguments => Invocation.Arguments; - - public Type[] GenericArguments => Invocation.GenericArguments; - - public object TargetObject => Invocation.InvocationTarget; - - public MethodInfo Method => Invocation.MethodInvocationTarget; - - public object ReturnValue - { - get => Invocation.ReturnValue; - set => Invocation.ReturnValue = value; - } - - protected IInvocation Invocation { get; } - - protected CastleAbpMethodInvocationAdapterBase(IInvocation invocation) - { - Invocation = invocation; - } - } -} \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpAsyncInterceptor.cs b/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpAsyncInterceptor.cs deleted file mode 100644 index dd5cadbc4c..0000000000 --- a/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpAsyncInterceptor.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Threading.Tasks; - -namespace Volo.Abp.DynamicProxy -{ - public interface IAbpAsyncInterceptor - { - Task InterceptAsync(IAbpAsyncMethodInvocation invocation); - } -} \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpAsyncMethodInvocation.cs b/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpAsyncMethodInvocation.cs deleted file mode 100644 index 560ad95821..0000000000 --- a/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpAsyncMethodInvocation.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Threading.Tasks; - -namespace Volo.Abp.DynamicProxy -{ - public interface IAbpAsyncMethodInvocation: IAbpMethodInvocationCore - { - Task ProceedAsync(); - } -} \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpInterceptor.cs b/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpInterceptor.cs index d8f2728fed..d6acec1151 100644 --- a/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpInterceptor.cs +++ b/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpInterceptor.cs @@ -1,7 +1,9 @@ -namespace Volo.Abp.DynamicProxy +using System.Threading.Tasks; + +namespace Volo.Abp.DynamicProxy { public interface IAbpInterceptor { - void Intercept(IAbpMethodInvocation invocation); + Task InterceptAsync(IAbpMethodInvocation invocation); } } diff --git a/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpMethodInvocation.cs b/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpMethodInvocation.cs index a7c22fe895..0f95b83d13 100644 --- a/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpMethodInvocation.cs +++ b/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpMethodInvocation.cs @@ -1,7 +1,21 @@ +using System; +using System.Reflection; +using System.Threading.Tasks; + namespace Volo.Abp.DynamicProxy { - public interface IAbpMethodInvocation: IAbpMethodInvocationCore + public interface IAbpMethodInvocation { - void Proceed(); + object[] Arguments { get; } + + Type[] GenericArguments { get; } + + object TargetObject { get; } + + MethodInfo Method { get; } + + object ReturnValue { get; set; } + + Task ProceedAsync(); } } \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpMethodInvocationCore.cs b/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpMethodInvocationCore.cs deleted file mode 100644 index bf5f34371f..0000000000 --- a/src/Volo.Abp/Volo/Abp/DynamicProxy/IAbpMethodInvocationCore.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Reflection; - -namespace Volo.Abp.DynamicProxy -{ - public interface IAbpMethodInvocationCore - { - object[] Arguments { get; } - - Type[] GenericArguments { get; } - - object TargetObject { get; } - - MethodInfo Method { get; } - - object ReturnValue { get; set; } - } -} \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs b/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs index 16a15ba585..396ba05681 100644 --- a/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs +++ b/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs @@ -1,11 +1,10 @@ using System.Threading.Tasks; using Volo.Abp.DynamicProxy; -using Volo.Abp.Threading; using Volo.DependencyInjection; namespace Volo.Abp.Uow { - public class UnitOfWorkInterceptor : IAbpInterceptor, IAbpAsyncInterceptor, ITransientDependency + public class UnitOfWorkInterceptor : IAbpInterceptor, ITransientDependency { private readonly IUnitOfWorkManager _unitOfWorkManager; @@ -13,19 +12,8 @@ namespace Volo.Abp.Uow { _unitOfWorkManager = unitOfWorkManager; } - - public void Intercept(IAbpMethodInvocation invocation) - { - //TODO: Check UOW attribute and other conditions! - using (var uow = _unitOfWorkManager.Begin()) - { - invocation.Proceed(); - uow.Complete(); - } - } - - public async Task InterceptAsync(IAbpAsyncMethodInvocation invocation) + public async Task InterceptAsync(IAbpMethodInvocation invocation) { //TODO: Check UOW attribute and other conditions! @@ -35,39 +23,5 @@ namespace Volo.Abp.Uow await uow.CompleteAsync(); } } - - - private void PerformAsyncUow(IAbpMethodInvocation invocation) - { - var uow = _unitOfWorkManager.Begin(); - - try - { - invocation.Proceed(); - } - catch - { - uow.Dispose(); - throw; - } - - if (invocation.Method.ReturnType == typeof(Task)) - { - invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally( - (Task)invocation.ReturnValue, - async () => await uow.CompleteAsync(), - exception => uow.Dispose() - ); - } - else //Task - { - invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult( - invocation.Method.ReturnType.GenericTypeArguments[0], - invocation.ReturnValue, - async () => await uow.CompleteAsync(), - exception => uow.Dispose() - ); - } - } } } 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 83d5a50dda..7c0ce926d3 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 @@ -20,7 +20,30 @@ namespace Volo.Abp.Autofac.Interception } [Fact] - public async Task Should_Intercept_Async_Methods() + public async Task Should_Intercept_Async_Method_Without_Return_Value() + { + //Arrange + + var target = ServiceProvider.GetService(); + + //Act + + await target.DoItAsync(); + + //Assert + + target.Logs.Count.ShouldBe(7); + target.Logs[0].ShouldBe("SimpleInterceptor_BeforeInvocation"); + target.Logs[1].ShouldBe("SimpleInterceptor2_BeforeInvocation"); + target.Logs[2].ShouldBe("EnterDoItAsync"); + target.Logs[3].ShouldBe("MiddleDoItAsync"); + target.Logs[4].ShouldBe("ExitDoItAsync"); + target.Logs[5].ShouldBe("SimpleInterceptor2_AfterInvocation"); + target.Logs[6].ShouldBe("SimpleInterceptor_AfterInvocation"); + } + + [Fact] + public async Task Should_Intercept_Async_Method_With_Return_Value() { //Arrange @@ -32,12 +55,57 @@ namespace Volo.Abp.Autofac.Interception //Assert + result.ShouldBe(42); + target.Logs.Count.ShouldBe(7); + target.Logs[0].ShouldBe("SimpleInterceptor_BeforeInvocation"); + target.Logs[1].ShouldBe("SimpleInterceptor2_BeforeInvocation"); + target.Logs[2].ShouldBe("EnterGetValueAsync"); + target.Logs[3].ShouldBe("MiddleGetValueAsync"); + target.Logs[4].ShouldBe("ExitGetValueAsync"); + target.Logs[5].ShouldBe("SimpleInterceptor2_AfterInvocation"); + target.Logs[6].ShouldBe("SimpleInterceptor_AfterInvocation"); + } + + [Fact] + public void Should_Intercept_Sync_Method_Without_Return_Value() + { + //Arrange + + var target = ServiceProvider.GetService(); + + //Act + + target.DoIt(); + + //Assert + + target.Logs.Count.ShouldBe(5); + target.Logs[0].ShouldBe("SimpleInterceptor_BeforeInvocation"); + target.Logs[1].ShouldBe("SimpleInterceptor2_BeforeInvocation"); + target.Logs[2].ShouldBe("ExecutingDoIt"); + target.Logs[3].ShouldBe("SimpleInterceptor2_AfterInvocation"); + target.Logs[4].ShouldBe("SimpleInterceptor_AfterInvocation"); + } + + [Fact] + public void Should_Intercept_Sync_Method_With_Return_Value() + { + //Arrange + + var target = ServiceProvider.GetService(); + + //Act + + var result = target.GetValue(); + + //Assert + result.ShouldBe(42); target.Logs.Count.ShouldBe(5); target.Logs[0].ShouldBe("SimpleInterceptor_BeforeInvocation"); - target.Logs[1].ShouldBe("EnterGetValueAsync"); - target.Logs[2].ShouldBe("MiddleGetValueAsync"); - target.Logs[3].ShouldBe("ExitGetValueAsync"); + target.Logs[1].ShouldBe("SimpleInterceptor2_BeforeInvocation"); + target.Logs[2].ShouldBe("ExecutingGetValue"); + target.Logs[3].ShouldBe("SimpleInterceptor2_AfterInvocation"); target.Logs[4].ShouldBe("SimpleInterceptor_AfterInvocation"); } } diff --git a/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/AbpCastleCoreTestModule.cs b/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/AbpCastleCoreTestModule.cs index 6bdfa007da..94d0a4785a 100644 --- a/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/AbpCastleCoreTestModule.cs +++ b/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/AbpCastleCoreTestModule.cs @@ -23,6 +23,7 @@ namespace Volo.Abp.Castle if (typeof(SimpleInterceptionTargetClass) == registration.ImplementationType) { registration.Interceptors.Add(); + registration.Interceptors.Add(); } } } 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 497d1a3fc0..6c81a2e0c0 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 @@ -11,24 +11,6 @@ namespace Volo.Abp.Castle.DynamicProxy public abstract class CastleInterceptionTestBase : AbpIntegratedTest where TStartupModule : IAbpModule { - [Fact] - public void Should_Intercept_Sync_Methods() - { - //Arrange - var target = ServiceProvider.GetService(); - - //Act - - var result = target.GetValue(); - - //Assert - - result.ShouldBe(42); - target.Logs.Count.ShouldBe(3); - target.Logs[0].ShouldBe("SimpleInterceptor_BeforeInvocation"); - target.Logs[1].ShouldBe("ExecutingGetValue"); - 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/SimpleInterceptionTargetClass.cs b/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/SimpleInterceptionTargetClass.cs index 9acf2b1832..74d0ac6d95 100644 --- a/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/SimpleInterceptionTargetClass.cs +++ b/test/Volo.Abp.Castle.Core.Tests/Volo/Abp/Castle/DynamicProxy/SimpleInterceptionTargetClass.cs @@ -9,6 +9,11 @@ namespace Volo.Abp.Castle.DynamicProxy { public List Logs { get; } = new List(); + public virtual void DoIt() + { + Logs.Add("ExecutingDoIt"); + } + public virtual int GetValue() { Logs.Add("ExecutingGetValue"); @@ -18,11 +23,20 @@ namespace Volo.Abp.Castle.DynamicProxy public virtual async Task GetValueAsync() { Logs.Add("EnterGetValueAsync"); - await Task.Delay(1); + await Task.Delay(5); Logs.Add("MiddleGetValueAsync"); - await Task.Delay(1); + await Task.Delay(5); Logs.Add("ExitGetValueAsync"); return 42; } + + public virtual async Task DoItAsync() + { + Logs.Add("EnterDoItAsync"); + await Task.Delay(5); + Logs.Add("MiddleDoItAsync"); + await Task.Delay(5); + Logs.Add("ExitDoItAsync"); + } } } \ 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 e900d9a667..3b6048d2d9 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 @@ -5,20 +5,27 @@ using Volo.DependencyInjection; namespace Volo.Abp.Castle.DynamicProxy { - public class SimpleInterceptor : IAbpInterceptor, /*IAbpAsyncInterceptor,*/ ITransientDependency + public class SimpleInterceptor : IAbpInterceptor, ITransientDependency { - public void Intercept(IAbpMethodInvocation invocation) + public async Task InterceptAsync(IAbpMethodInvocation invocation) { + //await Task.Delay(5); (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor_BeforeInvocation"); - invocation.Proceed(); + await invocation.ProceedAsync(); (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor_AfterInvocation"); + await Task.Delay(5); } + } - //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"); - //} + 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); + } } } \ No newline at end of file