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

Flowing Scoped Lifestyle and Scoped Collection resolve #948

Closed
Glunder0 opened this issue Jun 26, 2022 · 2 comments
Closed

Flowing Scoped Lifestyle and Scoped Collection resolve #948

Glunder0 opened this issue Jun 26, 2022 · 2 comments
Labels
Milestone

Comments

@Glunder0
Copy link

When DefaultScopedLifestyle is set to ScopedLifestyle.Flowing collection elements that are registered as Scoped can be only resolved from the first scope. Trying to resolve them in any consequent scope throws an exception.

var container = new Container();
container.Options.DefaultScopedLifestyle = ScopedLifestyle.Flowing;
container.Collection.Append<IInterface, Instance>(Lifestyle.Scoped);

using (var scope = new Scope(container))
{
    scope.GetInstance<IInterface[]>(); // Ok
}

using (var scope = new Scope(container))
{
    scope.GetInstance<IInterface[]>(); // Exception
}
Unhandled exception. SimpleInjector.ActivationException: Cannot access a disposed object.
Object name: 'SimpleInjector.Scope'.
 ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'SimpleInjector.Scope'.
   at SimpleInjector.Scope.ThrowObjectDisposedException()
   at SimpleInjector.Scope.GetInstanceInternal(ScopedRegistration registration)
   at SimpleInjector.Scope.GetInstance[TImplementation](ScopedRegistration registration, Scope scope)
   at SimpleInjector.Advanced.Internal.LazyScopedRegistration`1.GetInstance(Scope scope)
   at lambda_method7(Closure )
   at SimpleInjector.InstanceProducer.GetInstance()
   --- End of inner exception stack trace ---
   at SimpleInjector.InstanceProducer.GetInstance()
   at SimpleInjector.Internals.ContainerControlledCollection`1.CopyTo(TService[] array, Int32 arrayIndex)
   at SimpleInjector.Internals.FlowingContainerControlledCollection`1.CopyTo(TService[] array, Int32 arrayIndex)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at SimpleInjector.InstanceProducer.GetInstance()
   at SimpleInjector.Container.GetInstance(Type serviceType)
   at SimpleInjector.Scope.GetInstance(Type serviceType)
   at SimpleInjector.Scope.GetInstance[TService]()

And there is a difference in resolving scoped collections and resolving scoped single instances with DefaultScopedLifestyle set to ScopedLifestyle.Flowing.

var container = new Container();
container.Options.DefaultScopedLifestyle = ScopedLifestyle.Flowing;
container.Register<IInterface, Instance>(Lifestyle.Scoped);
container.Collection.Append<IInterface, Instance>(Lifestyle.Scoped);

var scopeOne = new Scope(container);
var singleInstanceOne = scopeOne.GetInstance<IInterface>();
var collectionInstanceOne = scopeOne.GetInstance<IInterface[]>()[0];

var scopeTwo = new Scope(container);
var singleInstanceTwo = scopeTwo.GetInstance<IInterface>();
var collectionInstanceTwo = scopeTwo.GetInstance<IInterface[]>()[0];

Console.WriteLine(singleInstanceOne.Equals(singleInstanceTwo)); // False
Console.WriteLine(collectionInstanceOne.Equals(collectionInstanceTwo)); // True
@dotnetjunkie
Copy link
Collaborator

Two bugs for the price of one. Thanks for these very clear and reproducible bug reports.

I've got some bad news though, which is that I don't have a work around available and it might take some time for me to publish a new release that fixes these issues. ETA mid July.

@dotnetjunkie dotnetjunkie added this to the Simple Injector v5.4 milestone Jun 26, 2022
dotnetjunkie added a commit that referenced this issue Jul 20, 2022
…container was set up using the flowing scoped lifestyle. Fixes #948
@dotnetjunkie
Copy link
Collaborator

This bug has been fixed in v5.4. It can be downloaded from NuGet.

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

No branches or pull requests

2 participants