简体   繁体   中英

Castle Windsor Transient Lifestyle Not Activating

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM