You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Different Flowing Scopes Returning Same Instance of a Scoped Object from within a Singleton constructor #966
I have two different Flowing Scopes, but they are returning the same instance of a scoped dependency.
To Reproduce
varcontainer=newContainer();container.Options.DefaultScopedLifestyle=ScopedLifestyle.Flowing;container.Register<Singleton>(Lifestyle.Singleton);container.Register<Scoped>(Lifestyle.Scoped);container.GetInstance<Singleton>();publicclassScoped{}publicclassSingleton{publicSingleton(Containerc){varscope1=newScope(c);varscope2=newScope(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.
changed the title [-]Different Flowing Scopes Returning Same Instance of a Scoped Object[/-][+]Different Flowing Scopes Returning Same Instance of a Scoped Object from within a Singleton constructor[/+]on Jan 11, 2023
Thanks for this repro. Using the supplied code I can confirm that this is a bug. I haven't fully investigated what the cause of the bug is, but what I see is that the problem only appear in a very specific situation, which is when instances are being resolved from within the constructor of a singleton component.
I don't have a solution yet, but a workaround is to either:
Make the component anything but singleton
Move the calls to GetInstance out of the constructor.
Set EnableAutoVerification to false.
Will try to fix this in the following patch or minor release.
Activity
[-]Different Flowing Scopes Returning Same Instance of a Scoped Object[/-][+]Different Flowing Scopes Returning Same Instance of a Scoped Object from within a Singleton constructor[/+]dotnetjunkie commentedon Jan 11, 2023
Thanks for this repro. Using the supplied code I can confirm that this is a bug. I haven't fully investigated what the cause of the bug is, but what I see is that the problem only appear in a very specific situation, which is when instances are being resolved from within the constructor of a singleton component.
I don't have a solution yet, but a workaround is to either:
GetInstanceout of the constructor.EnableAutoVerificationto false.Will try to fix this in the following patch or minor release.
Different flowing scopes returned the same instance of a scoped compo…
Fixed compile error. #966
dotnetjunkie commentedon Jan 5, 2024
Sorry for the delay of almost a year, but this bug finally got fixed in v5.4.4.