simpleinjector / SimpleInjector

An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
https://simpleinjector.org
MIT License
1.22k stars 152 forks source link

RegisterSingleton behaves inconsistently. #1008

Open dotnetjunkie opened 1 month ago

dotnetjunkie commented 1 month ago

Considering the following example:

container.Register(typeof(Foo<>)); // class Foo<T> { }
container.RegisterSingleton(typeof(Bar<>)); // class Bar<T> { }
// Class Consumer<T>(Foo<T> f, Bar<T> b) { }
container.Register<Consumer<int>>();
container.Verify();

Verification fails complaining that Bar<int> is not registered.

Reason for this is that the RegisterSingleton overload called is RegisterSingleton(Type openGenericServiceType, params Assembly[] assemblies), which means that the following call is made:

container.RegisterSingleton(openGenericServiceType: typeof(Bar<>), assemblies: new Assembly[0]);

This results in the assembly scanning of non-generic implementations of Bar<T> within zero assemblies.

This actually costed me 10 minutes to figure out what was going on. If this happens to me... it can happen to anyone.