Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity to dispose of object

Is there a way to make Unity dispose property-injected objects as part of the Teardown?

The background is that I am working on an application that uses ASP.NET MVC 2, Unity and WCF. We have written our own MVC controller factory that uses unity to instantiate the controller and WCF proxies are injected using the [Dependency] attribute on public properties of the controller. At the end of the page life cycle the ReleaseController method of the controller factory is called and we call IUnityContainer.Teardown(theMvcController). At that point the controller is disposed as expected but I also need to dispose the injected wcf-proxies. (Actually I need to call Close and/or Abort on them and not Dispose but that is a later problem.)

I could of course override the controllers' Dispose methods and clean up the proxies there, but I don't want the controllers to have to know about the lifecycles of the injected interfaces or even that they refer to WCF proxies.

If I need to write code myself for this - what would be the best extension point? I'd appreciate any pointer.

like image 763
Johan Levin Avatar asked Sep 18 '09 09:09

Johan Levin


Video Answer


1 Answers

I've created a unity extension that will take care of disposing instances created by the container on TearDown.

See http://www.neovolve.com/2010/06/18/unity-extension-for-disposing-build-trees-on-teardown/

like image 90
Rory Primrose Avatar answered Sep 27 '22 19:09

Rory Primrose