castle 动态代理(异步)
castle 异步方法拦截可以参见 https://github.com/castleproject/Core/issues/107
一 接口异步的,拦截器非异步
public interface IAsyncTInterface { Task<string> TestAsyncMethod(); } public class AsyncTarget : IAsyncTInterface { public async Task<string> TestAsyncMethod() { await Task.Delay(100); throw new Exception("Something failed, in target method"); } }
拦截器
public class MyInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { invocation.Proceed(); var returnValue = (Task<string>)invocation.ReturnValue; returnValue.ContinueWith(t => { Console.WriteLine(t.Exception.Message); invocation.ReturnValue = Task.FromResult("result"); }, TaskContinuationOptions.OnlyOnFaulted); } }
代理对象
var builder = new ContainerBuilder(); builder.RegisterType<AsyncTarget>() .As<IAsyncTInterface>() .EnableInterfaceInterceptors().InterceptedBy(typeof(MyInterceptor));
二 异步拦截器
castle拦截器没有实现对异步方法的拦截,异步方法拦截使用 Castle.Core.AsyncInterceptor 库来处理。Castle.Core.AsyncInterceptor库几个核心对象如下

IAsyncInterceptor 拦截器接口 ,AsyncDeterminationInterceptor充当适配器的角色,实现castle.core IInterceptor,方法执行会调用IAsyncInterceptor 。
实现步骤
1.先定义一个对象继承AsyncInterceptorBase
public class NewAsyncInterceptor : AsyncInterceptorBase { protected override async Task InterceptAsync( IInvocation invocation, IInvocationProceedInfo proceedInfo, Func<IInvocation, IInvocationProceedInfo, Task> proceed) { try { Console.WriteLine($"{invocation.Method.Name}:Starting Void Method"); await proceed(invocation, proceedInfo).ConfigureAwait(false); Console.WriteLine($"{invocation.Method.Name}:Completed Void Method"); } catch (Exception e) { Console.WriteLine($"{invocation.Method.Name}:Throw Exception :{e.Message}"); throw; } } protected override async Task<TResult> InterceptAsync<TResult>( IInvocation invocation, IInvocationProceedInfo proceedInfo, Func<IInvocation, IInvocationProceedInfo, Task<TResult>> proceed) { try { Console.WriteLine($"{invocation.Method.Name}:Starting Result Method"); TResult result = await proceed(invocation, proceedInfo).ConfigureAwait(false); Console.WriteLine($"{invocation.Method.Name}:Completed Result Method"); return result; } catch (Exception e) { Console.WriteLine($"{invocation.Method.Name}: Throw Exception Result:{e.Message}"); throw; } } }
2.适配器实现
castle对于异步注入,使用autofac 暂时是不支持直接注入IAsyncInterceptor 的,如果使用autofac注入castle.core 需要适配器来处理。
public class AsyncInterceptorAdaper : AsyncDeterminationInterceptor { IAsyncInterceptor _asyncInterceptor; public AsyncInterceptorAdaper(IAsyncInterceptor Interceptor) : base(Interceptor) { _asyncInterceptor = Interceptor; } public override void Intercept(IInvocation invocation) { _asyncInterceptor.ToInterceptor().Intercept(invocation); } }
3.注册
var builder = new ContainerBuilder(); builder.RegisterType<AsyncInterceptorAdaper>(); builder.RegisterType<NewAsyncInterceptor>().As<IAsyncInterceptor>(); builder.RegisterType<AsyncTarget>() .As<IAsyncTInterface>() .EnableInterfaceInterceptors().InterceptedBy(typeof(AsyncInterceptorAdaper));
泛型适配器
public class AsyncInterceptorAdaper<TAsyncInterceptor> : AsyncDeterminationInterceptor where TAsyncInterceptor : IAsyncInterceptor { public AsyncInterceptorAdaper(TAsyncInterceptor asyncInterceptor) : base(asyncInterceptor) { } }
var builder = new ContainerBuilder(); builder.RegisterType<NewAsyncInterceptor>(); builder.RegisterGeneric(typeof(AsyncInterceptorAdaper<>)); builder.RegisterType<AsyncTarget>() .As<IAsyncTInterface>() .EnableInterfaceInterceptors().InterceptedBy(typeof(AsyncInterceptorAdaper<>).MakeGenericType(typeof(NewAsyncInterceptor)));

浙公网安备 33010602011771号