0

我有一个IModelInterceptorsSelector一个WindsorContainer。除了没有实现的组件外(例如,所有行为都由IInterceptor动态处理),它都可以正常工作。温莎城堡为代理接口,而目标

如果我试图解决一个component接口而已,我得到:

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>() }; 
     } 
    } 

感谢。

+0

不知道你可以这样做与温莎和误读的问题,因为这一点。删除答案。 – 2010-10-31 21:24:10

回答

0

这绝对是温莎的一个bug。它看起来好像接受一个没有目标的组件的InterceptorGroup。那就是:

工程。

container.Register(Component.For<IInterfaceOnlyService> 
().Interceptors(InterceptorReference.ForType(typeof(TestInterceptor)))); 

在看着城堡源,所述不同的是,首先加入adescriptor直接:

return this.AddDescriptor(new InterceptorDescriptor<TService>(new 
InterceptorReference[] { new InterceptorReference(typeof(TInterceptor)) })); 

但第二个使用一个拦截器组内部:

return new InterceptorGroup<TService>((ComponentRegistration<TService>) this, 
interceptors); 

因此,本似乎是InterceptorGroups中的一个错误。

我的解决方法(虽然哈克)是用我LazyComponentLoader直接调用AddDescriptor。