从3.2.0升级到Castle Windsor 3.3.0时功能发生变化

时间:2014-06-17 03:27:57

标签: asp.net castle-windsor castle

我正在尝试从版本3.2.0迁移到3.3.0。我收到编译错误。我在“突破性变化”部分找不到一个条目,但这是我的两个错误,希望有人可以指导我找到一个可行的替代方案。

public void RegisterTypeSingleton<T>(Type component, string name)
{
    if (_container.Kernel.HasComponent(name))
        _container.Kernel.RemoveComponent(name);    
    _container.Register(Component.For<T>().ImplementedBy(component).Named(name).LifeStyle.Singleton);    
}
  1. 似乎Kernel.RemoveComponent()函数已被折旧。什么取代了这个?

  2. 第二个编译器错误位于_ container.Register(Component.For<T>().ImplementedBy(component).Named(name).LifeStyle.Singleton);

    • 我得到“Type'TService'必须是引用类型才能将其用作参数。

1 个答案:

答案 0 :(得分:4)

我认为您可能会从旧版本升级到3.2.0。见下文。

  1. IKernel.RemoveComponent()文档中记录了change - Removed the following methods: GraphNode.RemoveDepender, GraphNode.RemoveDependent, IKernel.RemoveComponent, IKernelEvents.ComponentUnregistered, INamingSubSystem.this[Type service], INamingSubSystem.GetHandler, INamingSubSystem.GetService2Handler, INamingSubSystem.GetKey2Handler, INamingSubSystem.UnRegister(String key), INamingSubSystem.UnRegister(Type service) Also INamingSubSystem.Register now takes only IHandler as its argument impact - low fixability - none description - The methods were implementation of "remove component from the container" feature which was flawed and problematic, hecen was scraped. fix - Working around is quite dependant on your specific usage. Try utilizing IHandlerSelectors. For changed Register method, just update your calling code not to pass the name. handler.ComponentModel.Name is now used as the key, as it was happening in all places so far anyway, so this change should have no real impact. 的删除,其中包含v3.0.0。以下是Krzysztof解释其删除原因的摘录:

    RegisterComponent()

    IsDefault()不会覆盖现有的服务注册,它只会注册同一服务的另一个组件,除非您指定相同的名称,它会抛出异常,通知您有另一个组件注册了那个名字。如果您的应用程序不经常更换组件,您可以在注册时使用IHandlerSelector方法让Windsor默认解析新组件,只需注意其他组件仍然已注册。

    如果您的应用程序经常更换组件而您不希望其他注册,那么您最好使用自定义ISubDependencyResolverpublic void RegisterTypeSingleton<T>(Type component, string name) where T : class { ... } ,以便Windsor每次都会询问您哪个组件希望用于特定服务。

  2. 同样在v3.0.0 Breaking Changes中确保a change was made。您需要向接受泛型参数的方法添加泛型约束,以便它也只接受引用类型:

    {{1}}