views:

741

answers:

2

Is there a way to make Unit 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.

+1  A: 

Cleanup should be the responsibility of the parent object, not the Unity Container that created them. IDisposable is meant to be used as part of a chain of responsibility pattern.

public class MyClass : IDisposable
{

     [Dependency]
     public MyWCFProxy Proxy { get; set; }

     public void Dispose()
     {

          try
          {
               Proxy.Dispose();
          }
          catch
          {
               //Do whatever here
          }

     }

}

Hope this helps.

Anderson Imes
The disposable pattern here is not correctly implemented...
Cecil Has a Name
Any elaboration?
Anderson Imes
The problem with this approach is that the parent object can't tell whether it shares the dependency with any other object.Autofac and now Windsor support this scenario properly, but my guess is that switching could be tough :)
Nicholas Blumhardt
I see your point now. If you had something that was ContainerControlledLifetimeManager, you might dispose it too quickly. Interesting issue... i'll have to think on this for a while. We have a similar issue right now that's ended up causing a few memory leaks due to unreleased resources.
Anderson Imes
Cecil, you're right that the correct disposal of a wcf proxy is a bit more elaborate than calling Dispose, but that is a different issue.My current workaround is to have my controller factory use reflection to find properties decorated with [Dependency] that implement IDisposable and call Dispose from ReleaseController. It is not perfect but it works.I have been playing with the option to set up a new Unity container for every request and make the proxies' lifetimes container controlled but that also seems like a bit of a workaround.
Johan Levin
FYI, I ended up writing a new lifetime manager (PerRequestLifetimeManager) that closes/aborts the wcf proxies at the end of the web request. That is probably a neater solution anyway...
Johan Levin
A: 

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

See http://www.neovolve.com/post/2010/06/18/Unity-Extension-For-Disposing-Build-Trees-On-TearDown.aspx

Rory Primrose