[Resolved] - User error

Mar 11, 2014 at 8:50 PM
I have a UnityContainer instance that I populate from configuration on [web] application startup (i.e., Application_Start() in Global.asax.cs) and then wrap in UnityServiceLocator.

At the end of the application lifecycle, I dispose the service locator wrapper - which I believe should dispose the underlying container as well - but the instances which have been registered with the "singleton" (A.K.A. ContainerControlledLifetimeManager) lifetime type are not being disposed.

I can see that the service locator is being disposed. I have tried debugging, and when I inspect the ContainerControlledLifetimeManager its private "value" field always seems to be null, which would explain why the instances aren't being disposed. What I can't figure out is why the lifetime manager doesn't have a reference to it's singleton.
Mar 12, 2014 at 3:17 PM
Yes, when you Dispose the ServiceLocator then the container is disposed which disposes all lifetime managers (that implement IDisposable). The lifetime managers then dispose the objects they contain if the objects also implelement IDisposable.

Perhaps what you are seeing is that the object has not been resolved so it is not being instantiated and later disposed?

Unity lazy loads objects that are registered with the ContainerControlledLifetimeManager so it will only build up a singleton class that needs to be resolved. If the object is not instantiated the ContainerControlledLifetimeManager value will be null and the object won't be disposed which seems to match the behavior you are seeing.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Mar 13, 2014 at 11:38 PM
Edited Mar 13, 2014 at 11:41 PM
That's not the case. I'm making sure I execute the code that resolves the instance(s) before I run through the disposal. That's why it's very strange to me that the lifetime manager instance doesn't have a reference to a value.
Mar 14, 2014 at 2:53 AM
Can you provide a sample project that illustrates the behavior you are seeing?

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Mar 14, 2014 at 9:11 PM
Will do
Mar 16, 2014 at 12:45 AM
Thanks for the feedback. In creating a sample project I was not able to reproduce the behavior. I added much more logging to my original solution and eventually determined that I had inadvertently set the LocatorProvider on the static ServiceLocator class to be the creation function for my service locator instead of the singleton instance of it. After fixing that code the singleton instances are now disposed properly. I'm sorry for wasting your time.