如何在调用Resolve时使CastleWindsor创建一个新实例

时间:2011-06-08 13:37:53

标签: c# castle-windsor ioc-container

我有以下代码:

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

和测试方法

[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);
}

我应该更改测试才能通过?

1 个答案:

答案 0 :(得分:5)

将生活方式设置为Transient

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

默认生活方式为Singleton,这就是您看到相同实例的原因。