Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed factory out of order disposal #486

Open
MishaD87 opened this issue May 2, 2019 · 0 comments
Open

Typed factory out of order disposal #486

MishaD87 opened this issue May 2, 2019 · 0 comments

Comments

@MishaD87
Copy link

MishaD87 commented May 2, 2019

Hello!
I think this issue is related to this one
#344
Code below gives "The factory was disposed and can no longer be used." exception in "Dispose" method of 'MyFactoryUser'. Tested on latest version of Castle Windsor (5.0.0)

 static void Main(string[] args)
        {
            using (var provider = new Provider())
            {
                var factoryUser = provider.GetFactoryUser();
            }
        }

 public class Installer : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(Component.For(typeof(IMyFactory)).AsFactory().LifestyleSingleton(),
                Component.For<IMyObject>()
                    .ImplementedBy<MyObject>()
                    .LifestyleSingleton(),
                Component.For<IMyFactoryUser>()
                    .ImplementedBy<MyFactoryUser>()
                    .LifestyleTransient());
        }
    }

    public class Provider : IDisposable
    {
        readonly IWindsorContainer _container;

        public Provider()
        {
            _container = CreateContainer();
        }

        static WindsorContainer CreateContainer()
        {
            var container = new WindsorContainer();
            container.AddFacility<TypedFactoryFacility>();
            container.Install(new Installer());
            return container;
        }

        public void Dispose()
        {
            _container?.Dispose();
        }

        public IMyFactoryUser GetFactoryUser() => _container.Resolve<IMyFactoryUser>();
    }

    public interface IMyFactory
    {
        IMyObject Create();
        void Release(IMyObject instance);
    }

    public interface IMyObject
    {
    }

    public class MyObject : IMyObject
    {
    }

    public interface IMyFactoryUser
    {
    }

    public class MyFactoryUser : IMyFactoryUser, IDisposable
    {
        private readonly IMyFactory _myFactory;
        private readonly IMyObject _myObject;

        public MyFactoryUser(IMyFactory myFactory)
        {
            _myFactory = myFactory;
            _myObject = myFactory.Create();
        }

        public void Dispose()
        {
            _myFactory.Release(_myObject);
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant