Container not being registered with itself

Jun 16, 2009 at 1:46 PM

My understanding was that in Unity 1.2, the container was automatically registered with itself, but if I comment out the line where I manually register it, it won't work.

Here is some test code that fails when the RegisterInstance is commented out:

 

 

            UnityContainer unity = new UnityContainer();
            //unity.RegisterInstance<UnityContainer>(unity);
            var x = unity.Resolve<UnityContainer>();
            Debug.Assert(x.GetHashCode() == unity.GetHashCode());

Am I misunderstanding the auto-registration feature of Unity 1.2? Is there some code I can look for in the Unity source I have to verify that I have the correct Unity code?

 

Jun 16, 2009 at 5:10 PM

Ray, that's very strange. I just ran this code:

            var container = new UnityContainer();
            var resolved = container.Resolve<IUnityContainer>();
            if(container.GetHashCode() == resolved.GetHashCode())
            {
                Console.WriteLine("Same Container");
            }
            else
            {
                Console.WriteLine("Different container");
            }

And got "Same Container" out. Stock Unity 1.2.

Look at the DefaultBehaviorExtension.cs file, line 32. It should read:

            Container.RegisterInstance<IUnityContainer>(Container, new ContainerLifetimeManager());

If it doesn't, you probably have Unity 1.0 or 1.1, not 1.2.

Or you can double check the version number in the properties of the .DLL file.

Hope this helps,

-Chris

Jun 16, 2009 at 7:06 PM

OK. I  got it. I was Resolve()ing for UnityContainer instead of IUnityContainer.