castle-windsor – 删除Castle Windsor 3中的组件

我在Castle Windsor使用TypedFactoryFacility允许我使用接口 – 工厂依赖注入.

我遇到自动委托工厂将Func注入自动解析组件时出现问题(如果不需要)(应该是Null).

我想保留TypedFactoryFacility,但删除DelegateFactory,根据这个问题:

Can Windsor’s TypedFactoryFacility’s implicit delegate factory registration be disabled?

不幸的是,现在没有办法从Castle Windsor(版本3)中删除组件.

有人可以建议一种方法来删除DelegateFactory或以某种方式禁用它,以便它不会将Func注入我无法解析的服务(为什么它甚至注入Func,它不知道如何处理?)

最佳答案 我无法找到如何删除组件(这应该重新添加).

我发现禁用DelegateFactory的最好方法是停止使用TypedFactoryFacility类来设置Typed Factories并使用其中的Init函数中的代码,减去委托工厂方法.

如此:

// Stop using AddFacility
//container.AddFacility<TypedFactoryFacility>();

// Initialise the TypedFactoryFacility manually, leaving out the DelegateFactory components.
container.Kernel.Register(new IRegistration[] {
   Component.For<TypedFactoryInterceptor>().NamedAutomatically(TypedFactoryFacility.InterceptorKey),
   // Disable DelegateFactory
   // Component.For<ILazyComponentLoader>().ImplementedBy<DelegateFactory>().NamedAutomatically(TypedFactoryFacility.DelegateFactoryKey),
   Component.For<ITypedFactoryComponentSelector>().ImplementedBy<DefaultTypedFactoryComponentSelector>().NamedAutomatically("Castle.TypedFactory.DefaultInterfaceFactoryComponentSelector"),
   // Disable DelegateFactory
   // Component.For<ITypedFactoryComponentSelector>().ImplementedBy<DefaultDelegateComponentSelector>().NamedAutomatically(TypedFactoryFacility.DefaultDelegateSelectorKey)
});
container.Kernel.ComponentModelBuilder.AddContributor(new TypedFactoryCachingInspector());

我不得不使用魔术字符串“Castle.TypedFactory.DefaultInterfaceFactoryComponentSelector”DefaultInterfaceSelectorKey是一个内部字段.

现在你可以使用接口工厂,而不需要委托工厂搞乱一切.

点赞