views:

329

answers:

1

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.

Thanks.

A: 

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.

Mauricio Scheffer
Thanks for the workaround, can confirm it works. Will report as a bug as advised.
crowleym
Bug report: http://support.castleproject.org/projects/IOC/issues/view/IOC-ISSUE-160Logged in using OpenID, and it seems to have logged as reported by another account (jsimons)...but none the less it is the report that stemmed from this post.
crowleym

related questions