温莎城堡短暂生活方式未激活

发布于 2024-09-07 18:29:11 字数 1248 浏览 6 评论 0原文

我的容器中的组件存在瞬态生活方式的问题。第一次调用resolve时,我按预期命中了实现类型的构造函数。

但是,第二次调用resolve时,没有构造新实例。相反,现有实例被重用。我认为这种情况不应该发生,因为我的组件的 LifestyleType 设置为 Transient(我已在运行时在断点处以调试模式验证了这一点):

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 只是返回 Component.For(service)

默认为 Singleton LifestyleType (即使它在 ComponentModel 上显示为 Unknown。这是设计使然吗?

谢谢。

I'm having a problem with a component in my container with a Transient lifestyle. The first time I call resolve, I hit the constructor of the implementation type as expected.

However, the second time I call resolve, a new instance is not constructed. Rather, the existing instance is reused. I don't think this should happen, since my component's LifestyleType is set to Transient (I have verified this at runtime in debugging mode at a breakpoint):

Kernel.GetAssignableHandlers(typeof(object))[33].ComponentModel.LifestyleType 
// 33 is the verified index of my component type...this returns Transient as expected

At the same breakpoint, I have run the following in the immediate window and verified that a new instance is not constructed:

  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.

Update:

I've narrowed the problem down, I think.

The below test fails:

    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 simply returns
Component.For(service)

which is defaulting to Singleton LifestyleType (even though it shows up as Unknown on the ComponentModel. Is this by design?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦初启 2024-09-14 18:29:11

Per Krzysztof Koźmic:是的,默认的生活方式是单例,未知意味着它没有明确指定,但 Windsor 将其视为您指定了单例。如果您希望它是暂时的,请明确说明。

Per Krzysztof Koźmic: Yes, the default lifestyle is singleton, Unknown means it's not specified explicitly, but Windsor treats it as if you specified Singleton. If you want it to be transient, be explicit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文