Dependency injection 温莎拦截器例外
我有一个Windsor容器,我正在使用拦截器选择器和LazyComponentLoader 我的InterceptorSelector返回对我的InterceptorAdapter类的InterceptorReference,如下所示Dependency injection 温莎拦截器例外,dependency-injection,inversion-of-control,castle-windsor,interceptor,Dependency Injection,Inversion Of Control,Castle Windsor,Interceptor,我有一个Windsor容器,我正在使用拦截器选择器和LazyComponentLoader 我的InterceptorSelector返回对我的InterceptorAdapter类的InterceptorReference,如下所示 public class InterceptorAdapter<T> : Castle.DynamicProxy.IInterceptor, IOnBehalfAware where T : IMyType { private readonly
public class InterceptorAdapter<T> : Castle.DynamicProxy.IInterceptor, IOnBehalfAware where T : IMyType
{
private readonly T interceptor;
public InterceptorAdapter(T interceptor)
{
this.interceptor = interceptor;
}
....
}
我的LazyComponentLoader就是这么做的
public IRegistration Load(string key, Type service, IDictionary arguments)
{
ComponentRegistration<object> component = Component.For(service).Named(key);
if (arguments != null)
{
// merge is an extension method to merge dictionaries.
component.DynamicParameters((k, d) => d.Merge(arguments));
}
return component;
}
公共IRegistration加载(字符串键、类型服务、IDictionary参数)
{
ComponentRegistration组件=组件.For(服务).Named(键);
if(参数!=null)
{
//merge是合并词典的扩展方法。
component.DynamicParameters((k,d)=>d.Merge(参数));
}
返回组件;
}
我的容器设置看起来像
var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.AddFacility("factories", new FactorySupportFacility());
container.Register(Component.For<ILazyComponentLoader>().ImplementedBy<MyLazyComponentLoader>().LifeStyle.Singleton);
container.Kernel.ProxyFactory.AddInterceptorSelector(new InterceptorSelector(container.Kernel, container.Resolve<ILazyComponentLoader>()));
var容器=新的WindsorContainer();
container.AddFacility();
container.AddFacility(“工厂”,新的FactorySupportFacility());
container.Register(Component.For().ImplementedBy().lifety.Singleton);
AddInterceptorSelector(新的拦截器选择器(container.Kernel,container.Resolve());
有什么建议吗
谢谢 这似乎是Castle使用LazyComponentLoader解决拦截器的一个问题 在选择器中更改我的代码以执行以下操作:
if (kernel.GetHandler(interceptorType) == null)
{
// need to do this or Castle complains it can't create my
// interceptor...I guess LazyComponentLoaders don't work for InterceptorReferences?...
// I suspect the problem may be here...
kernel.Register(lazyComponentLoader.Load(null, interceptorType, null));
}
Type interceptorAdapterType = typeof(InterceptorAdapter<>).MakeGenericType(interceptorType);
if (kernel.GetHandler(interceptorAdapterType) == null)
{
// need to do this or Castle complains it can't create my
// interceptor...I guess LazyComponentLoaders don't work for InterceptorReferences?...
// I suspect the problem may be here...
kernel.Register(lazyComponentLoader.Load(null, interceptorAdapterType, null));
}
if(kernel.GetHandler(interceptorType)==null)
{
//需要这样做,否则Castle抱怨它无法创建我的
//拦截器…我猜LazyComponentLoader不适用于拦截器引用?。。。
//我怀疑问题可能在这里。。。
Register(lazyComponentLoader.Load(null,interceptorType,null));
}
类型interceptorAdapterType=typeof(InterceptorAdapter)。MakeGenericType(interceptorType);
if(kernel.GetHandler(interceptorAdapterType)==null)
{
//需要这样做,否则Castle抱怨它无法创建我的
//拦截器…我猜LazyComponentLoader不适用于拦截器引用?。。。
//我怀疑问题可能在这里。。。
Register(lazyComponentLoader.Load(null,interceptorAdapterType,null));
}
似乎解决了这个问题
var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.AddFacility("factories", new FactorySupportFacility());
container.Register(Component.For<ILazyComponentLoader>().ImplementedBy<MyLazyComponentLoader>().LifeStyle.Singleton);
container.Kernel.ProxyFactory.AddInterceptorSelector(new InterceptorSelector(container.Kernel, container.Resolve<ILazyComponentLoader>()));
if (kernel.GetHandler(interceptorType) == null)
{
// need to do this or Castle complains it can't create my
// interceptor...I guess LazyComponentLoaders don't work for InterceptorReferences?...
// I suspect the problem may be here...
kernel.Register(lazyComponentLoader.Load(null, interceptorType, null));
}
Type interceptorAdapterType = typeof(InterceptorAdapter<>).MakeGenericType(interceptorType);
if (kernel.GetHandler(interceptorAdapterType) == null)
{
// need to do this or Castle complains it can't create my
// interceptor...I guess LazyComponentLoaders don't work for InterceptorReferences?...
// I suspect the problem may be here...
kernel.Register(lazyComponentLoader.Load(null, interceptorAdapterType, null));
}