Issue with generics and LifetimeManagers

Oct 3, 2011 at 4:02 PM

I love the simplicity of Munq, but I've encountered a pretty big problem with generic types and any of the lifetime managers that caches the reference using a string key (so ThreadLocalStorage, Request, Session, and Cache).

Simple example of failing code:

var lifetime = new Munq.LifetimeManagers.ThreadLocalStorageLifetime();
var container = new Munq.IocContainer();

container.Register<IFoo<int>, Foo<int>>().WithLifetimeManager(lifetime);
container.Register<IFoo<string>, Foo<string>>().WithLifetimeManager(lifetime);

Assert.IsNotNull(container.Resolve<IFoo<int>>());     // works
Assert.IsNotNull(container.Resolve<IFoo<string>>());  // fails

The issue is that the key generated for the type is based off of Type.Name (line 31 of Registration.cs), which in the case of a generic type returns the same string for all types (in my example, both IFoo<int> and IFoo<string> return "IFoo`1" for their Name).  When the lifetime retrieves them from the cache, it casts them using "as", which returns null since it is not the right type.

I believe the solution is as simple as using type.FullName instead of type.Name.

Oct 4, 2011 at 11:43 PM


Of course it should use FullName.  I'll update the source and the NuGet package.

Did you try the change?



Oct 5, 2011 at 6:59 PM

Yes, I downloaded the source and patched it and all seems well.

Oct 5, 2011 at 9:52 PM
I added your code as a test. I'll try and update everything this weekend.

From: dfullerton
Sent: 05/10/2011 1:59 PM
Subject: Re: Issue with generics and LifetimeManagers [Munq:274599]

From: dfullerton

Yes, I downloaded the source and patched it and all seems well.

Oct 20, 2011 at 5:00 PM

I've updated the source with your patches.  Thanks for the catch.



Nov 5, 2011 at 10:43 PM

Included in the latest release on NuGet