温莎容器:如何强制处置对象的? [英] Windsor Container: How to force dispose of an object?

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

问题描述

我有一个对象,它实现了注册到温莎集装箱,我想处理它,所以它调用Dispose方法,并在下一次解决被称为它获取一个新的实例IDisposable的。

I have an object that implements IDisposable that is registered with the Windsor Container and I would like to dispose of it so it's Dispose method is called and next time Resolve is called it fetches a new instance.

确实

container.Release(obj);

自动调用Dispose()立刻?或者,我需要做的。

automatically call Dispose() immediately? Or do I need to do

obj.Dispose();
container.Release(obj);

无法找到的文档中的任何究竟是什么版本呢

Couldn't find anything in the documentation on what exactly Release does

编辑: 见我的回答如下的测试我跑的结果。现在的问题是,如何强制容器释放组件的实例有一个单身的生命周期?这个只需要在一个地方,编写自定义的生命周期做似乎过于重量级,有没有内置在做这件事的方法是什么?

See my answer below for the results of tests I ran. Now the question becomes, how do I force the container to release an instance of a component with a singleton lifecycle? This only needs to be done in one place and writing a custom lifecycle seems far too heavyweight, is there no built in way of doing it?

推荐答案

这是我认为人们没有真正意识到与温莎货柜装卸时 - 尤其是经常的奇怪行为的一次性瞬时组件扶住由容器为内核的生命周期,直到它的处置,除非你自己释放出来 - 尽管它记录 - 看看<一href="http://www.castleproject.org/container/documentation/trunk/advanced/releasepolicy.html">here - 但快速报价:

This is something I think people aren't really aware of when working with the Windsor container - especially the often surprising behavior that disposable transient components are held onto by the container for the lifetime of the kernel until it's disposed unless you release them yourself - though it is documented - take a look here - but to quickly quote:

微内核有一个可插拔的发布策略,可以连接起来,实现一些   路由处置组件。微内核带有三个IReleasePolicy实现:

the MicroKernel has a pluggable release policy that can hook up and implement some routing to dispose the components. The MicroKernel comes with three IReleasePolicy implementations:

      
  • AllComponentsReleasePolicy:跟踪所有组件在微内核实例处理执行正确的处理
  •   
  • LifecycledComponentsReleasePolicy:只有有退役的生命周期相关联的轨道部件
  •   
  • NoTrackingReleasePolicy:不执行任何跟踪
  •   
  • AllComponentsReleasePolicy: track all components to enforce correct disposal upon the MicroKernel instance disposal
  • LifecycledComponentsReleasePolicy: only track components that have a decommission lifecycle associated
  • NoTrackingReleasePolicy: does not perform any tracking

您也可以通过该接口IReleasePolicy实现自己的发行策略。

You can also implement your own release policy by using the interface IReleasePolicy.

你可能会发现更容易是政策改变为 NoTrackingReleasePolicy ,然后处理处置自己 - 这是潜在的风险为好,但如果你的生活方式在很大程度上是短暂的(或者当你的容器配置你的应用程序即将结束反正)它可能不是一个大问题。但是,请记住那个已经被注射了单身的任何组件将举行一个参考,所以你可能会最终导致试图刷新你的单身问题 - 这似乎是一个不好的做法,我不知道你也许能避免不必通过改进的方法您的应用程序放在一起做摆在首位。

What you might find easier is to change the policy to a NoTrackingReleasePolicy and then handle the disposing yourself - this is potentially risky as well, but if your lifestyles are largely transient (or if when your container is disposed your application is about to close anyway) it's probably not a big deal. Remember however that any components which have already been injected with the singleton will hold a reference, so you could end up causing problems trying to "refresh" your singletons - it seems like a bad practice, and I wonder if perhaps you can avoid having to do this in the first place by improving the way your applications put together.

其他的方法是建立一个自定义的生命周期它自己退役的实现(所以释放的单身实际上处理的组件,很像短暂的生命周期一样)。

Other approaches are to build a custom lifecycle with it's own decommission implementation (so releasing the singleton would actually dispose of the component, much like the transient lifecycle does).

另外一种方法是有一个装饰为您服务登记在容器单身生活,但登记在盛有过性生活的实际的基础服务 - 那么当你需要刷新组件刚刚处理了瞬时基本的装饰持有组件,并将其与新鲜解决的实例来替换(使用组件的关键,而不是服务,以避免让装饰解决它) - 这避免了与其他单身人士服务的问题(这是不被刷新 )从持有到已处理使之无法使用的陈旧的服务,但需要一点等的铸造,使其工作。

Alternatively another approach is to have a decorator for your service registered in the container with a singleton lifestyle, but your actual underlying service registered in the container with a transient lifestyle - then when you need to refresh the component just dispose of the transient underlying component held by the decorator and replace it with a freshly resolved instance (resolve it using the components key, rather then the service, to avoid getting the decorator) - this avoids issues with other singleton services (which aren't being "refreshed") from holding onto stale services which have been disposed of making them unusable, but does require a bit of casting etc. to make it work.

这篇关于温莎容器:如何强制处置对象的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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