Unity 处理对象

发布于 2024-08-05 07:09:22 字数 471 浏览 5 评论 0原文

有没有办法让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

婴鹅 2024-08-12 07:09:22

我创建了一个统一扩展,它将负责处理 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/

盛夏尉蓝 2024-08-12 07:09:22

一种可能的解决方法是,您还可以在代理周围编写一个包装器,该包装器将在处置(处置实例时由 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 ?

苍景流年 2024-08-12 07:09:22

获得 UnityDependencyResolver 后,

GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);

您就可以在解析器上调用 Dispose。在 ASP.NET 中,您可以选择从 Global.asax.cs Application_End 方法中调用此方法,如下所示:

GlobalConfiguration.Configuration.DependencyResolver.Dispose();

然后,这将对具有容器生命周期的所有内容调用 dispose,例如添加到容器中的实例:

var myFooInstance = new Foo();
container.RegisterInstance<IFoo>(myFooInstance);

Once you've got your UnityDependencyResolver

GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);

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:

GlobalConfiguration.Configuration.DependencyResolver.Dispose();

This will then call dispose on all the stuff that have container lifetime, such as instances added to the container with:

var myFooInstance = new Foo();
container.RegisterInstance<IFoo>(myFooInstance);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文