统一处置对象 [英] Unity to dispose of object

查看:58
本文介绍了统一处置对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以使Unity在拆解过程中处理注入属性的对象?

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

背景是我正在开发一个使用ASP.NET MVC 2,Unity和WCF的应用程序.我们已经编写了自己的MVC控制器工厂,该工厂使用unity实例化控制器,并使用控制器的公共属性上的[Dependency]属性注入WCF代理.在页面生命周期结束时,将调用控制器工厂的ReleaseController方法,并且我们将调用IUnityContainer.Teardown(theMvcController).到那时,控制器已按预期配置,但我还需要配置注入的wcf-proxies. (实际上,我需要对它们调用关闭"和/或中止",而不是处置",但这是以后的问题.)

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.)

我当然可以覆盖控制器的Dispose方法并在那里清理代理,但是我不希望控制器必须了解注入接口的生命周期,甚至不知道它们引用的是WCF代理.

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.

推荐答案

我创建了一个unity扩展,它将处理在TearDown上处理由容器创建的实例.

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

请参见 http ://www.neovolve.com/2010/06/18/unity-extension-for-dispose-build-trees-on-teardown/

这篇关于统一处置对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆