【问题标题】:Castle Windsor Transient Lifestyle Not Activating温莎城堡瞬态生活方式未激活
【发布时间】:2010-07-04 00:04:31
【问题描述】:

我的容器中的组件存在 Transient 生活方式问题。第一次调用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(服务)

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

谢谢。

【问题讨论】:

  • 感觉不对。你能在测试中重现它吗?
  • 是的,我已经更新了描述。谢谢。
  • 是的,默认的生活方式是单例,未知意味着它没有明确指定,但温莎把它当作你指定了单例。如果您希望它是短暂的,请明确。

标签: dependency-injection inversion-of-control castle-windsor


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    相关资源
    最近更新 更多