Implemented async interception.

pull/81/head
Halil İbrahim Kalkan 9 years ago
parent ed53a95e12
commit 370d0e3b1c

@ -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<IAbpAsyncInterceptor>().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));
}
}
}

@ -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();
}
}
}
}

@ -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");
}
}
}

@ -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<TStartupModule> : AbpIntegratedTest<TStartupModule>
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");
}
}
}

@ -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");
//}
}
}
Loading…
Cancel
Save