Clearing down resources used in a singleton

Nov 18, 2008 at 9:06 PM
Edited Nov 18, 2008 at 9:18 PM
Within a module we create a singleton

_container.RegisterType<IDocumentWorkflowRepository, DocumentWorkflowRepository>(new ContainerControlledLifetimeManager());

However within this we'd like to dispose of a resource the singleton uses. What's the best way to achieve this?

 The boot strapper is created in the app on startup event which has a reference to unity container. Should I keep a reference to the bootstrapper or unity container and then call dispose in an override such as OnExit?

Should my singleton object implement IDisposable which presumably the disposal of the unity container would call?

Nov 19, 2008 at 11:32 AM
Singleton object and its lifetime is managed by container. The singlton object is kept in scope as long as the container isn’t disposed.

To go around on this I think would be easiest to create hiarchy of containers. By adding child container to main container. Each container added become child of parent container. You can disponse any of the child container independently. Gil Fink on his blog descibes container hierarchies on Unity, see the link below. 
Nov 19, 2008 at 12:55 PM
Thanks Alexander

However why do I need the complexity of hierarchy of containers if you mentioned

"The singlton object is kept in scope as long as the container isn’t disposed."

Can't I simply dispose of the main container? If so what's the recommend way to do this in a WPF appi.e. at what point?
Nov 19, 2008 at 2:13 PM
Edited Nov 19, 2008 at 3:32 PM


I wasn’t sure exactly what you were looking for in your question so I made some assumptions, so lets start again.

If you going to use UnityBootStrapper you can not dispose your main Unity container, or you can but then your application will be gone as well.

If you need to to dispose your singleton objects without leaving the application (if you do leave they are gone). You can do this by two different ways:

  • You can create a child container and register type mapping/instances with ContainerControlledLifeTimeManger and dispose this child container when you are required to dispose the singleton objects without leaving the application.


  • You can also try to use Unity ExternallyControlledLifetimeManager instead of ContainerControlledLifeTimeManager. I am not sure if Silverlight version of Unity is supporting this but you can try. Anyhow, you can simply dispose these objects by setting those type instances to null.

About ContainerControlledLifeTimeManager:
If you create a singleton object by using ContainerControlledLifeTimeManager the Unity container will create a strong reference to it. Which will mean that the container will take over management of the object  lifetime. The only way to dispose your (singleton) object(s) from container in this case is to dispose the container that holds a singleton object or exit the application.

About ExternallyControlledLifeTimeManager:
When you use ExternallyControlledLifeTimeManager the Unity container will returns the same instance of the registered type or object each time you call the Resolve method. The container does not hold onto a strong reference to the object after it creates it, which means that the garbage collector can dispose of the object if no other code is holding a strong reference to it.

Hope this helps and clears out the answer.


Jun 24, 2010 at 4:56 PM
Hi Could you please provide some code sample for both scenarios? Perhaps for the situation when you register an instance of scoped regionmanager in the container