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

Collection abstractions don't get verified unless collection is consumed #690

Open
dotnetjunkie opened this issue Apr 25, 2019 · 1 comment

Comments

Projects
None yet
1 participant
@dotnetjunkie
Copy link
Collaborator

commented Apr 25, 2019

Considering the following code:

public interface IFoo { }

var container = new Container();
container.Collection.Register(typeof(IFoo), new[] { typeof(IFoo) });
container.Verify();

In the previous snippet, IFoo is registered as item in the IFoo collection. Although this is valid, it does require IFoo to be registered as one-to-one mapping as well, e.g.:

container.Register<IFoo, DefaultFoo>();

Expected behavior

I'd expect the an exception to be thrown similar to the following:

No registration for type Program.IFoo could be found.

Actual behavior

Instead, when running the first snippet, the Verify() method succeeds. Only after the collection is consumed by another component, an exception is thrown:

public interface IFoo { }
public class Consumer { public Consumer(IEnumerable<IFoo> foos) { } }

var container = new Container();
container.Collection.Register(typeof(IFoo), new[] { typeof(IFoo) });
container.Register<Consumer>();
container.Verify();

In this case, the following exception is thrown:

The configuration is invalid. Creating the instance for type Program.IFoo failed. No registration for type Program.IFoo could be found. There is, however, a registration for IEnumerable<IFoo>; Did you mean to call GetAllInstances<IFoo>() or depend on IEnumerable<IFoo>? Please see https://simpleinjector.org/collections for more information about registering and resolving collections.

Additional context

  • Library version: Simple Injector v4.5.1

@dotnetjunkie dotnetjunkie added the bug label Apr 25, 2019

@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 26, 2019

bug-690 branch created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.