Unity to dispose of object

微笑、不失礼 提交于 2019-12-08 18:14:02

问题


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.


回答1:


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/




回答2:


A possible workaround is that you could also write a wrapper around your proxies that will on dispose (called by Unity when disposing instances) call a Close method of the proxy. Is that a viable scenarion for you ?



来源:https://stackoverflow.com/questions/1443515/unity-to-dispose-of-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!