Dependency injection 温莎城堡暂时生活方式未激活
我的容器中有一个组件有问题,它的生活方式是短暂的。第一次调用resolve时,我按预期命中了实现类型的构造函数 但是,第二次调用resolve时,没有构造新实例。而是重用现有实例。我认为不应该发生这种情况,因为我的组件的Lifestyle Type设置为Transient,我在运行时以调试模式在断点处验证了这一点:Dependency injection 温莎城堡暂时生活方式未激活,dependency-injection,inversion-of-control,castle-windsor,Dependency Injection,Inversion Of Control,Castle Windsor,我的容器中有一个组件有问题,它的生活方式是短暂的。第一次调用resolve时,我按预期命中了实现类型的构造函数 但是,第二次调用resolve时,没有构造新实例。而是重用现有实例。我认为不应该发生这种情况,因为我的组件的Lifestyle Type设置为Transient,我在运行时以调试模式在断点处验证了这一点: Kernel.GetAssignableHandlers(typeof(object))[33].ComponentModel.LifestyleType // 33 is the
Kernel.GetAssignableHandlers(typeof(object))[33].ComponentModel.LifestyleType
// 33 is the verified index of my component type...this returns Transient as expected
在同一断点处,我在即时窗口中运行了以下命令,并验证了未构造新实例:
Resolve(Kernel.GetAssignableHandlers(typeof(object))[33].Service)
// this does NOT return a new instance!
// ...It returns the same instance from the first time Resolve was called.
// I can tell by the state of the object and because the constructor on the object is not called.
更新:
我想我已经把问题缩小了
以下测试失败:
var container = new WindsorContainer();
container.Kernel.AddComponent<MyLazyComponentLoader>(typeof (ILazyComponentLoader));
var instance1 = container.Resolve<MyClass>();
var instance2 = container.Resolve<MyClass>();
Assert.AreNotSame(instance1, instance2);
MyLazyComponentLoader只返回
组件服务
它默认为Singleton Lifestyle类型,即使它在ComponentModel上显示为未知。这是故意的吗
谢谢。Per Krzysztof Koźmic:是的,默认的生活方式是单身,未知意味着没有明确指定,但温莎将其视为您指定的单身。如果你想让它是暂时的,那就直截了当。这感觉不对。你能在测试中重现它吗?是的,我已经更新了描述。谢谢。是的,默认的生活方式是单身,未知意味着没有明确指定,但温莎将其视为您指定的单身。如果你想让它是暂时的,就要明确。