ResolutionFailedException in a Controller with more than one Dependency

Sep 25, 2011 at 12:46 AM

Hello,

I have a controller with a contructor like Controlle([Dependency] D1Abstraction d1, [Dependency] D2Abstraction d2, [Dependency] D3Abstraction d3) so when the Unity go try resolve he throw this Exception is: InvalidCastException - Unable to cast object of type 'D1Abstraction' to type 'D2Abstraction'. I regester the types correctly like container.RegisterType<D1Abstraction, D1Implementation>().RegisterType<D2Abstraction, D2Implementation>().RegisterType<D3Abstraction, D3Implementation>();

So i ask, why Unity are getting confused?

Sorry by the english.

Sep 25, 2011 at 5:22 AM
Edited Sep 25, 2011 at 3:29 PM

 

I found out what was happening, when I registered the types in the container, i used ...HttpContextLifetimeManager(new EF4Context()));, and i did that many times. So i added a Guid in the implementation of HttpContextLifetimeManager and is now working.
Like
private readonly string Key;
 
        public HttpContextLifetimeManager() 
        {
            Key = typeof(T).AssemblyQualifiedName + Guid.NewGuid().ToString();
        }
 
        public override object GetValue()
        {
            return HttpContext.Current.Items[Key];
        }
        public override void RemoveValue()
        {
            HttpContext.Current.Items.Remove(Key);
        }
        public override void SetValue(object newValue)
        {
            HttpContext.Current.Items[Key] = newValue;
        }
        public void Dispose()
        {
            RemoveValue();
        }