Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make CastleWindsor create a new instance when Resolve is called

I have the following code:

public interface IService { }
public class MyService : IService { }

and a test method

[Test]
public void T1()
{
    IWindsorContainer container = new WindsorContainer();

    container.Register(Component.For<IService>()
        .ImplementedBy<MyService>());

    var s1 = container.Resolve<IService>();
    var s2 = container.Resolve<IService>();
    Assert.AreNotSame(s1, s2);
}

What should I change for the test to pass?

like image 225
StuffHappens Avatar asked Sep 05 '25 03:09

StuffHappens


1 Answers

Set the lifestyle to Transient:

container.Register(
    Component.For<IService>()
             .ImplementedBy<MyService>()
             .LifeStyle.Transient
);

The default lifestyle is Singleton and that's why you see the same instance.

like image 198
jason Avatar answered Sep 07 '25 21:09

jason