I have registered an 'open-generic type', as per below:
_container.RegisterSingleOpenGeneric(
typeof(StandardCacheItemByKeyRetriever<>),
typeof(StandardCacheItemByKeyRetriever<>));
I then try to retrieve the instance twice as per below:
var t1 = _container.GetInstance<StandardCacheItemByKeyRetriever<SettingData>>();
var t2 = _container.GetInstance<StandardCacheItemByKeyRetriever<SettingData>>();
Shouldn't t1
be equal to t2
, considering I am using the RegisterSingleOpenGeneric
?
In the below, b is equal to false
- If it's the same, it should be true, right?
bool b = t1 == t2;
UPDATE: This is a bug which is now fixed in Simple Injector 2.2.
Unfortunately, you stumbled upon a bug into Simple Injector. The RegisterSingleOpenGeneric
doesn't handle your case (where the service type is equal to the implementation type) correctly. This results in the registration to be skipped completely, making the container fall back to its default behavior, which is creating concrete types with the transient lifestyle.
I'm working at a bug fix as we speak. Expect a new version (2.2) very soon. I will update this answer when it is released.
In the meantime, as a workaround, what you can do is registering the type by an interface, for instance:
_container.RegisterSingleOpenGeneric(
typeof(ICacheItemByKeyRetriever<>),
typeof(StandardCacheItemByKeyRetriever<>));
Another workaround (which is less pleasant) would be to do all registrations manually:
_container.RegisterSingle<StandardCacheItemByKeyRetriever<SettingData>>();
_container.RegisterSingle<StandardCacheItemByKeyRetriever<SomeData>>();
_container.RegisterSingle<StandardCacheItemByKeyRetriever<OtherData>>();
// etc
I'm sorry that you happened to stumble upon this.