我有以下代码:
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);
}
我应该更改测试才能通过?
答案 0 :(得分:5)
将生活方式设置为Transient
:
container.Register(
Component.For<IService>()
.ImplementedBy<MyService>()
.LifeStyle.Transient
);
默认生活方式为Singleton
,这就是您看到相同实例的原因。