castle-windsorwindsor-facilities

Why Castle Windsor's Typed Factory registered as Singleton lifestyle is Disposed?


I have very strange situation with Castle Windsor where factory registered as LifeStyle.Singleton appears disposed at some point. After couple of hours I was able to simplify production case to this excerpt:

EDIT After some investigation yet I've simplified issue to almost trivial case (original version saved below):

public interface IFactory {
    IAnotherFactory CreateAnother();
}

public interface IAnotherFactory {
    A CreateA();
}

public class A {}

[Test]
public void IsItABug() {
    Container.Kernel.AddFacility<TypedFactoryFacility>();
    Container.Register(Component.For<A>().LifestyleTransient(),
                       Component.For<IFactory>().AsFactory().LifestyleTransient(),
                       Component.For<IAnotherFactory>().AsFactory().LifestyleSingleton());

    // uncomment this line to make test pass
    // var makeAnotherUsedInMainContainerScopeBeforeFactoryCreation =
    //     Сontainer.Resolve<IAnotherFactory>();

    var factory = Container.Resolve<IFactory>();
    factory.CreateAnother();
    Container.Release(factory);
    var another = Container.Resolve<IAnotherFactory>();
    another.CreateA(); // throws ObjectDisposedException
}

Ant the exception is much shorter now as well:

System.ObjectDisposedException : The factory was disposed and can no longer be used.
Object name: 'this'.
   at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation invocation) in TypedFactoryInterceptor.cs: line 59
   at Castle.DynamicProxy.AbstractInvocation.Proceed() in d:\work\16de7b8c88ab14af\src\Castle.Core\DynamicProxy\AbstractInvocation.cs: line 145
   at Castle.Proxies.IAnotherFactoryProxy.CreateA()
   at Castle.Windsor.Tests.Facilities.TypedFactory.Components.TypedFactoryAndSubContainersTestCase.IsItABug() in TypedFactoryAndSubContainersTestCase.cs: line 102

I believe now it is not a question anymore, because Windsor has had very similar issue in the past (IOC-345), so I've written failing test and submitted an issue

Original variant:

public interface IFactory {
    Y CreateY();
    Z CreateZ();
    void Release(Y obj);
}

public class X {
    public X(IFactory factory) {
        var y = factory.CreateY();
        factory.Release(y);
        factory.CreateY();
    }
}

public class Y {
    public Y(IFactory factory) {
        factory.CreateZ();
    }
}

public class Z {
    public Z(IAnotherFactory anotherFactory) {
        anotherFactory.CreateA(); // <--- on second time anotherFactory is disposed already O_o
    }
}

public interface IAnotherFactory {
    A CreateA();
}

public class A {}

[TestFixture]
public class Class1
{
    [Test]
    public void IsItABug()
    {
        var c = new WindsorContainer();
        c.Kernel.AddFacility<TypedFactoryFacility>();
        c.Register(Component.For<X>().LifestyleTransient(),
                   Component.For<Y>().LifestyleTransient(),
                   Component.For<Z>().LifestyleTransient(),
                   Component.For<A>().LifestyleTransient(),
                   Component.For<IFactory>().AsFactory().LifestyleTransient(),
                   Component.For<IAnotherFactory>().AsFactory().LifestyleSingleton());

        c.Resolve<X>();
    }
}

On the marked line above with Windsor 3.2 I get the exception:

Castle.MicroKernel.ComponentActivator.ComponentActivatorException : ComponentActivator: could not instantiate Sandbox.GM.Config.X
  ----> System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> Castle.MicroKernel.ComponentActivator.ComponentActivatorException : ComponentActivator: could not instantiate Sandbox.GM.Config.Y
  ----> System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> Castle.MicroKernel.ComponentActivator.ComponentActivatorException : ComponentActivator: could not instantiate Sandbox.GM.Config.Z
  ----> System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> System.ObjectDisposedException : The factory was disposed and can no longer be used.
Object name: 'this'.

   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstanceCore(ConstructorCandidate constructor, Object[] arguments, Type implType)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext context, ConstructorCandidate constructor, Object[] arguments)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
   at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden)
   at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally)
   at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
   at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, ref Burden burden)
   at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
   at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context)
   at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
   at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
   at Castle.MicroKernel.DefaultKernel.Resolve(Type service, IDictionary arguments)
   at Castle.Windsor.WindsorContainer.Resolve()
   at TestApp.Class1.IsItABug() in Class1.cs: line 56
--TargetInvocationException
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.FastCreateInstance(Type implType, Object[] arguments, ConstructorCandidate constructor)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstanceCore(ConstructorCandidate constructor, Object[] arguments, Type implType)
--ComponentActivatorException
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstanceCore(ConstructorCandidate constructor, Object[] arguments, Type implType)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext context, ConstructorCandidate constructor, Object[] arguments)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
   at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden)
   at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally)
   at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
   at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, ref Burden burden)
   at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
   at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context)
   at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
   at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
   at Castle.Facilities.TypedFactory.TypedFactoryComponentResolver.Resolve(IKernelInternal kernel, IReleasePolicy scope)
   at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve(IInvocation invocation)
   at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.IFactoryProxy.CreateY()
   at TestApp.X..ctor(IFactory factory) in Class1.cs: line 19
--TargetInvocationException
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.FastCreateInstance(Type implType, Object[] arguments, ConstructorCandidate constructor)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstanceCore(ConstructorCandidate constructor, Object[] arguments, Type implType)
--ComponentActivatorException
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstanceCore(ConstructorCandidate constructor, Object[] arguments, Type implType)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext context, ConstructorCandidate constructor, Object[] arguments)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
   at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden)
   at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally)
   at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
   at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, ref Burden burden)
   at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
   at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context)
   at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
   at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
   at Castle.Facilities.TypedFactory.TypedFactoryComponentResolver.Resolve(IKernelInternal kernel, IReleasePolicy scope)
   at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve(IInvocation invocation)
   at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.IFactoryProxy.CreateZ()
   at TestApp.Y..ctor(IFactory factory) in Class1.cs: line 25
--TargetInvocationException
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.FastCreateInstance(Type implType, Object[] arguments, ConstructorCandidate constructor)
   at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstanceCore(ConstructorCandidate constructor, Object[] arguments, Type implType)
--ObjectDisposedException
   at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.IAnotherFactoryProxy.CreateA()
   at TestApp.Z..ctor(IAnotherFactory anotherFactory) in Class1.cs: line 31

Any ideas?


Solution

  • I strongly believe now it is a bug, hence the issue: https://github.com/castleproject/Windsor/pull/61