Skip to content

Different Flowing Scopes Returning Same Instance of a Scoped Object from within a Singleton constructor #966

@pnicoletti88

Description

@pnicoletti88

I have two different Flowing Scopes, but they are returning the same instance of a scoped dependency.

To Reproduce

var container = new Container();
container.Options.DefaultScopedLifestyle = ScopedLifestyle.Flowing;
container.Register<Singleton>(Lifestyle.Singleton);
container.Register<Scoped>(Lifestyle.Scoped);

container.GetInstance<Singleton>();

public class Scoped { }

public class Singleton
{
    public Singleton(Container c)
    {
        var scope1 = new Scope(c);
        var scope2 = new Scope(c);
    
        if (object.ReferenceEquals(scope1.GetInstance<Scoped>(), scope2.GetInstance<Scoped>()))
        {
            Assert.Fail("Resolved the same instance.");
        }
    } 
}

In this example the Assert.Fail is being hit. I do not believe this is expected.

Additional context

  • Adding container.Options.EnableAutoVerification = false; fixes the problem.
  • Running against latest version, 5.4.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions