没有目标的温莎城堡代理接口
我有一个带有 IModelInterceptorsSelector
的 WindsorContainer
。它工作得很好,除了没有实现的组件(例如,所有行为都由 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>() };
}
}
谢谢。
I have a WindsorContainer
with a IModelInterceptorsSelector
. It works well except for component's that have no implementation (eg. have all behavior handled dynamically by an IInterceptor
).
If I try to resolve a component with an interface only, I get:
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)
But if I manually specify the interceptor at registration time, it works just fine. Is this a bug in Windsor or am I doing something wrong?
The code to reproduce is fairly straightforward:
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>() };
}
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这绝对是温莎的一个错误。看起来它与接受没有目标的组件的 InterceptorGroup 有关。那就是:
作品。
查看 Castle 源代码,不同之处在于第一个直接添加描述符:
但第二个在内部使用拦截器组:
因此,这似乎是 InterceptorGroups 中的错误。
我的解决方法(虽然很hacky)是使用我的 LazyComponentLoader 直接调用 AddDescriptor。
This definitely seems to be a bug in Windsor. It looks like it has to do with accepting an InterceptorGroup for components without a target. That is:
works.
In looking at the Castle source, the difference is that the first adds adescriptor directly:
But the second uses an interceptor group internally:
Accordingly, this seems to be a bug in InterceptorGroups.
My workaround (although hacky) is to use my LazyComponentLoader to call AddDescriptor directly.