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

Dispose order with function factories #556

Closed
toserk opened this issue Mar 23, 2021 · 1 comment · Fixed by #557
Closed

Dispose order with function factories #556

toserk opened this issue Mar 23, 2021 · 1 comment · Fixed by #557
Labels

Comments

@toserk
Copy link

toserk commented Mar 23, 2021

I am trying to do something like this:

static void Main(string[] args)
{
    var container = new ServiceContainer();
    container.Register<Dep1>(new PerContainerLifetime());
    
    // If I use container.Register<Dep2>(new PerContainerLifetime()); dispose order is as expected.
    container.Register(factory => new Dep2(factory.GetInstance<Dep1>()), new PerContainerLifetime());
    
    container.Register<Dep3>(new PerContainerLifetime());
    container.GetInstance<Dep3>();
    container.Dispose();
    Console.ReadKey();
}

public class Dep1 : IDisposable
{
    private bool _disposed;

    public void Dispose()
    {
        Console.WriteLine("Dispose 1");
        _disposed = true;
    }

    public void DoSomething()
    {
        if (_disposed)
            Console.WriteLine("Fail");
    }
}

public class Dep2
{
    private readonly Dep1 _dep;

    public Dep2(Dep1 dep)
    {
        _dep = dep;
    }

    public void DoSomething()
    {
        _dep.DoSomething();
    }
}

public class Dep3 : IDisposable
{
    private readonly Dep2 _dep;

    public Dep3(Dep2 dep)
    {
        _dep = dep;
    }

    public void Dispose()
    {
        Console.WriteLine("Dispose 3");
        _dep.DoSomething();
    }
}

I expected dispose order be like this: Dep3.Dispose(), Dep1.Dispose().
But it actualy is Dep1.Dispose(), Dep3.Dispose()

@seesharper seesharper added the Bug label Mar 25, 2021
@seesharper
Copy link
Owner

Hi @toserk Thanks for reporting this. This is most certainly a bug. Looking into it 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants