温莎城堡:重新注册一个命名组件维持以前的生活方式

时间:2009-06-16 12:05:44

标签: castle-windsor

以下单元测试失败了,我正在寻找一个有效的理由。

interface IFoo { }
class Foo : IFoo { }
class Foo2 : IFoo { }

[TestMethod]
public void LifestyleTest4()
{
    WindsorContainer container = new WindsorContainer();

    container.Register(Component.For<IFoo>().ImplementedBy<Foo>().Named("foo").LifeStyle.Singleton);

    IHandler h = container.Kernel.GetHandler("foo");
    Assert.IsTrue(h.ComponentModel.LifestyleType == LifestyleType.Singleton);

    bool removed = container.Kernel.RemoveComponent("foo");
    Assert.IsTrue(removed);

    container.Register(Component.For<IFoo>().ImplementedBy<Foo2>().Named("foo").LifeStyle.Transient);

    h = container.Kernel.GetHandler("foo");
    //Assert will fail as LifestyleType == Singleton
    Assert.IsTrue(h.ComponentModel.LifestyleType == LifestyleType.Transient, "Expected Transient Lifestyle");
}

即使组件已从容器中删除,似乎仍按名称维护对LifeStyle的引用。

我正在使用2.0版本。

1 个答案:

答案 0 :(得分:2)

Krzysztof是对的,这看起来像个bug。

原因是包含生活方式的组件配置被RemoveComponent 删除,因此将始终使用第一个生活方式配置。

解决方法:在调用RemoveComponent()后添加此行:

container.Kernel.ConfigurationStore.GetComponentConfiguration("foo").Attributes.Remove("lifestyle");

请将其报告给问题跟踪器(包括您的测试用例)并在此处链接回来,这可以作为修复错误的起点。