如何在具有构造函数参数的IInterceptorSelector.SelectInterceptors方法中使用拦截器。我想让Autofac使用它的参数来解析我的拦截器,就像在这个Castle框架中一样:
InterceptorReference.ForType<CallLogger>()
我对此进行了研究,但一无所获。
以下是来自示例的示例代码:
class Program
{
static void Main(string[] args)
{
var builder = new ContainerBuilder();
var proxyGenerationOptions = new ProxyGenerationOptions();
//I want to use this
//proxyGenerationOptions.Selector = new InterceptorSelector();
builder.RegisterType<SomeType>()
.As<ISomeInterface>()
.EnableInterfaceInterceptors(proxyGenerationOptions)
.InterceptedBy(typeof(CallLogger));//and remove explicit statement
builder.Register<TextWriter>(x => Console.Out);
builder.RegisterType<CallLogger>().AsSelf();
var container = builder.Build();
var willBeIntercepted = container.Resolve<ISomeInterface>();
willBeIntercepted.Work();
}
}
public class InterceptorSelector : IInterceptorSelector
{
public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors)
{
//don't know how to solve dependency here, because it's registration time
return new IInterceptor[] { /* new CallLogger(dependency) or InterceptorReference.ForType<CallLogger>() */};
}
}
public class CallLogger : IInterceptor
{
TextWriter _output;
public CallLogger(TextWriter output)
{
_output = output;
}
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
_output.WriteLine("Done: result was {0}.", invocation.ReturnValue);
}
}
public interface ISomeInterface { void Work(); }
public class SomeType : ISomeInterface { public void Work() { } }
我也想知道,Autofac中是否有任何动态拦截器分配机制。在Castle中,有多种方法可以修改拦截管道。
答案 0 :(得分:1)
现在,在Autofac.Extras.DynamicProxy中这是不可能的。您可以see in the source从注册的元数据属性(由IntereceptedBy
放置)中获取拦截器,而不使用拦截器选择器。
我们有one user note,您可以连接自己的拦截器,执行以下操作:
builder.RegisterType<Implementation>().AsSelf();
builder.Register(c =>
{
ProxyGenerator proxyGen = new ProxyGenerator(true);
return proxyGen.CreateInterfaceProxyWithTargetInterface<IInterfaceOfImplementation>(
c.Resolve<Implementation>(),
c.Resolve<ExceptionCatcherInterceptor>());
}).As<IInterfaceOfImplementation>();
这是更多手动手册,但它可能会带您前往目的地。