TypedFactory refuses before the component uses it as a dependency

I have an interface and a typed factory interface:

public interface ITransientItem : IDisposable
{
    void DoWork(WorkItem item);
}

public interface ITransientItemFactory : IDisposable
{
    ITransientItem Create();
    void Destroy(ITransientItem item);
}

I Then implement the implementation of another interface IDependencyOwner : IDisposable, which is implemented using:

public class DependencyOwner: IDependencyOwner
{
    private ITransientItemFactory _factory;

    public DependencyOwner(ITransientItemFactory factory)
    {
        _factory = factory;
    }

    public void PostWork(WorkItem workItem)
    {
        ITransientItem item = _factory.Create();

        item.DoWork(workItem); //this is done on a seperate thread

        _factory.Destroy(item);
    }

    public void Dispose()
    {
        //first wait for running items to dispose

       //then do disposal stuff
    }
}

DependencyOwnerheld by a dependency for another object, and there may be many DependencyOwner implementations that are resolved with Sub Resolver CollectionResolver. but I do not think that this is relevant to this problem. Its constructor looks like this:

public TopLevel(IDependencyOwner[] dependencies)

Registering the container is as follows:

    WindsorContainer container = new WindsorContainer();

    container.AddFacility(new TypedFactoryFacility());
    container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel));

    container.Register(Component.For<TopLevel>());
    container.Register(Component.For<IDependencyOwner>().ImplementedBy<DependencyOwner>();
    //there will be more IDependencyOwner Implementations in the future

    container.Register(Component.For<ITransientItem>().ImplementedBy<TransientItem>()
        .LifeStyle.Transient);

    container.Register(Component.For<ITransientItemFactory>().AsFactory());

    TopLevel top = container.Resolve<TopLevel>();

Everything that actually works is great. The problem arises when it is time to close the program.

ITransientItemFactory , DependencyOwner Dispose ( , dispose, , , ), , , , , , .

:

System.ObjectDisposedException: factory . : 'this'.

Windsor ?

EDIT: , factory DependencyOwner.

EDIT 2: factory factory. ( ), , . :

WindsorContainer container = new WindsorContainer();

    //container.AddFacility(new TypedFactoryFacility());
    container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel));

    container.Register(Component.For<TopLevel>());
    container.Register(Component.For<IDependencyOwner>().ImplementedBy<DependencyOwner>();
    //there will be more IDependencyOwner Implementations in the future
    //No reason to register it anymore, it will never be instantiated by the container
    //container.Register(Component.For<ITransientItem>().ImplementedBy<TransientItem>()
        //.LifeStyle.Transient);

    //container.Register(Component.For<ITransientItemFactory>().AsFactory());
    container.Register(Component.For<ITransientItemFactory>()
        .ImplementedBy<FactoryImplementation>());

    TopLevel top = container.Resolve<TopLevel>();
+4

Source: https://habr.com/ru/post/1620795/


All Articles