does default LifeTimeManager support RemoveValue ?

Apr 13, 2012 at 6:28 AM

using Unity 2

want to implement UnregisterInstance

fount this http://unity.codeplex.com/discussions/241450

tried it, doens't work.

            UnityContainer uc = default container...

            XX x = uc.Resolve<XX>();
            uc.RegisterInstance<XX>(x);
//1st round
            foreach (var reg in uc.Registrations)
            {
                var b1 = reg.RegisteredType == x.GetType();
                if (b1)
                {
                    reg.LifetimeManager.RemoveValue();
                }
            }

//2nd round
            foreach (var reg in uc.Registrations)
            {
                var b1 = reg.RegisteredType == x.GetType();
                if (b1)
                {
                    ...
                }
            }

I'm using the default container (gotten from the bootstrapper)

1st round, the code went in b1, expected. there's code suppose to remove/unregister the instance.

but 2nd round, the code still wint in b1

does unregistering instance really worked in unity? or just bunch of hack?

Apr 14, 2012 at 8:23 AM

LifetimeManager.RemoveValue() will set the object's value to null (and if applicable call Dispose on the object).  But that does not remove it from the list of types Registered in the container.  You could look at using child containers where you register the instance in the child container and when no longer needed the child container is disposed.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com