Castle Windsor: Re-registration of a Named component maintains previous Lifestyle
-
05-07-2019 - |
Question
The below Unit Test fails, and I am looking for a valid reason for it doing so.
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");
}
Even though the component was removed from the container, it appears that a reference to the LifeStyle is maintained by name.
I am using the 2.0 release.
Solution
Krzysztof is right, this looks like a bug.
The reason is that the component configuration, which contains the lifestyle, is not removed by RemoveComponent, so the first lifestyle configuration will be always used.
Workaround: add this line after calling RemoveComponent():
container.Kernel.ConfigurationStore.GetComponentConfiguration("foo").Attributes.Remove("lifestyle");
Please report it to the issue tracker (include your testcase) and link back here, this could serve as a starting point to fix the bug.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow