【问题标题】:Castle Windsor Proxy for Interface without Target无目标接口的 Castle Windsor 代理
【发布时间】:2011-05-02 23:58:09
【问题描述】:

我有一个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>() };
        }
    }

谢谢。

【问题讨论】:

  • 不知道您可以在 Windsor 上做到这一点,并因此误读了这个问题。已删除答案。

标签: .net inversion-of-control castle-windsor castle-dynamicproxy


【解决方案1】:

这绝对是 Windsor 中的一个错误。看起来它与接受没有目标的组件的 InterceptorGroup 有关。那就是:

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

有效。

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

在看Castle源码的时候,不同的是第一个直接加了descriptor:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多