I meet one problem that i can't solve now. I have the following:
UnityHelper.DefaultContainer.RegisterInstance(typeof(IMyInterface), "test", instance);
where UnityHelper.DefaultContainer
is my helper for getting unity container with loaded configuration.
here I registered instance
as an instance of IMyInterface
.
So anywhere( some time after using) I want to remove this mapping. Remove it at all. How I can do it?
I have tried:
UnityHelper.DefaultContainer.Teardown(instance)
but is was unsuccessful and the following code returns instance
anyway:
UnityHelper.DefaultContainer.ResolveAll<IMyInterface>()
I had the same problem and just removed the registrations of the
ContainerControlledLifetimeManager
from myContainer
:This is an old question, but some answers are misleading, so I will provide my own. You can´t do that with Unity. End of the story. Calling
RemoveValue
on registrations lifetime managers does not achieve unregistration (more information about lifetime managers), and that method is not intended to unregister anything. So the final behaviour is unexpected and not convenient. Of course,RemoveValue
makes even less sense if you register an implementation or a factory method, although the question is about unregistering instances. Consider the next piece of codeIf you debug it, you will see that after the call to
ClearValue
, the container still says it is registered, but if you try to resolve that instance it will throw an exception. What is even worse, calls toResolveAll<T>
will fail too.To Sum up, no matter if you do
ClearValue
, wrap around your register instance with another IoC or a custom class, or provide your own LifeTimeManager,ResolveAll<T>
andIsRegistered<T>
won´t behave as expected, and the registration will still be there. So don't try it because it won´t work and it will cause problems down the road.I had a similar requirement whereby I wanted to temporarily store objects in the unity container and found this was not possible (or at least easily possible). If your objective is to have a temporary storage place easily available to unity, then create a temporary storage service.
Register your service with Unity. Then when you wish to store an object you call the Deposit Method and when you wish to remove the object you call the Withdraw method. A fuller explanation can be found here
I have the same challenge and after experimenting I solved it by using the standard ContainerControlledLifetimeManager and calling RemoveValue when I want to remove the container instance. Note that if you are not using interfaces and your object has constructor which the container can find and use it will recreate the instance after you have destroyed it with lifetimeManager.RemoveValue().
Here is how I handled unregistering instances from a unity container
I needed to implement Add/Remove functionality like this:
I created a custom lifetime manager to do the implementation
Here is the final implementation:
calling removevalue on the custom lifetime manager causes the instance to be unregistered
I think that is what you are looking for.