我有WindsorContainer
一个IModelInterceptorsSelector
。除了没有实现的组件(例如,所有行为都由 动态处理IInterceptor
)之外,它运行良好。
如果我尝试仅使用接口解析组件,我会得到:
Castle.MicroKernel.ComponentActivator.ComponentActivatorException occurred
Message=Could not find a public constructor for type ConsoleApplication1.IInterfaceOnlyService. Windsor can not instantiate types that don't expose public constructors. To expose the type as a service add public constructor, or use custom component activator.
Source=Castle.Windsor
StackTrace:
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.FastCreateInstance(Type implType, Object[] arguments, Type[] signature)
但是如果我在注册时手动指定拦截器,它工作得很好。这是温莎的错误还是我做错了什么?
重现的代码相当简单:
var container = new WindsorContainer();
container.Kernel.ProxyFactory.AddInterceptorSelector(new Selector());
container.Register(Component.For<TestInterceptor>());
container.Register(Component.For<IInterfaceOnlyService>()); // this doesn't work
// container.Register(Component.For<IInterfaceOnlyService>().Interceptors<TestInterceptor>()); // this works
var i = container.Resolve<IInterfaceOnlyService>();
public class Selector : IModelInterceptorsSelector
{
public bool HasInterceptors(ComponentModel model)
{
return model.Service != typeof (TestInterceptor);
}
public InterceptorReference[] SelectInterceptors(ComponentModel model, InterceptorReference[] interceptors)
{
return new[] { InterceptorReference.ForType<TestInterceptor>() };
}
}
谢谢。