Unity 处理对象
有没有办法让 Unity 将属性注入对象作为 Teardown 的一部分进行处理?
背景是我正在开发一个使用 ASP.NET MVC 2、Unity 和 WCF 的应用程序。我们编写了自己的 MVC 控制器工厂,它使用 unity 来实例化控制器,并使用控制器公共属性上的 [Dependency] 属性注入 WCF 代理。在页面生命周期结束时,控制器工厂的 ReleaseController 方法被调用,我们调用 IUnityContainer.Teardown(theMvcController)。此时控制器已按预期处置,但我还需要处置注入的 wcf 代理。 (实际上我需要对它们调用 Close 和/或 Abort,而不是 Dispose,但这是以后的问题。)
我当然可以覆盖控制器的 Dispose 方法并清理那里的代理,但我不希望控制器必须了解注入接口的生命周期,甚至它们引用 WCF 代理。
如果我需要自己为此编写代码 - 最好的扩展点是什么?我将不胜感激任何指点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我创建了一个统一扩展,它将负责处理 TearDown 上容器创建的实例。
请参阅 http:// www.neovolve.com/2010/06/18/unity-extension-for-disusing-build-trees-on-teardown/
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/
一种可能的解决方法是,您还可以在代理周围编写一个包装器,该包装器将在处置(处置实例时由 Unity 调用)调用代理的 Close 方法。这对您来说是可行的方案吗?
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 ?
获得 UnityDependencyResolver 后,
您就可以在解析器上调用 Dispose。在 ASP.NET 中,您可以选择从 Global.asax.cs Application_End 方法中调用此方法,如下所示:
然后,这将对具有容器生命周期的所有内容调用 dispose,例如添加到容器中的实例:
Once you've got your UnityDependencyResolver
You can then call Dispose on the resolver. In ASP.NET you might choose to call this from your Global.asax.cs Application_End method like this:
This will then call dispose on all the stuff that have container lifetime, such as instances added to the container with: