Castle Windsor:我的瞬态组件会被垃圾收集吗?

发布于 2024-07-14 05:31:11 字数 861 浏览 9 评论 0原文

使用 Castle Windsor,我有一个配置了瞬态生活方式的组件:

<component id="publish.mapping.default"
                   service="IMyService, MyAssembly"
                   type="MyServiceImplementation, Myassembly" 
                   lifestyle="transient" />

它将像这样使用:

var service = container.Resolve<IMyService>(componentId);
// service usage ....
// service goes out of scope ... 

我的问题是,服务实例在超出范围后是否会被垃圾收集,或者 Castle Windsor 会保留引用吗? 我发现这个类似的问题,这意味着后者可能情况确实如此 - 但在检查了那里发布的链接后,我不确定讨论是关于保留引用,还是关于确保对象在实现 IDisposable 的情况下被处置。 我的物品不需要被处置。

如果温莎城堡保留该实例,是否有任何简单的方法可以防止这种情况(也许通过配置)?

编辑
看来我需要设置发布跟踪策略。 可以在xml配置文件中配置,还是需要在代码中设置? 是否可以针对每个组件设置发布跟踪策略?

Using Castle Windsor, I have a component configured with the transient lifestyle:

<component id="publish.mapping.default"
                   service="IMyService, MyAssembly"
                   type="MyServiceImplementation, Myassembly" 
                   lifestyle="transient" />

Which will be used like this:

var service = container.Resolve<IMyService>(componentId);
// service usage ....
// service goes out of scope ... 

My question is, will the service instance be garbage collected after it goes out of scope, or will Castle Windsor hold on to a reference ? I found this similar question, that implies that the latter might be the case - but after examining the links posted there, I am unsure whether the discussion is about holding on to the reference, or about ensuring that the object are disposed if it implements IDisposable. My objects does not need to be disposed.

If Castle Windsor holds on to the instance, is there any easy way to prevent this (perhaps by configuration) ?

EDIT
It seems, that I need to set the release tracking policy. Can this be configured in the xml config file, or does it need to be set in code ? Can the release tracking policy be set on a per-component basis ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

动听の歌 2024-07-21 05:31:11

默认情况下,容器保存对对象(甚至是瞬态对象)的引用。

然而,正如 @Bittercoder 在 为什么温莎城堡会持有瞬态对象? ,您可以更改发布跟踪策略。 似乎选择了

LifecycledComponentsReleasePolicy:

var policy = container.Kernel.ReleasePolicy;
container.Kernel.ReleasePolicy = LifecycledComponentsReleasePolicy;

但自从提出问题以来,这似乎已成为默认策略。

By default, the container holds a reference to your objects (even the transient ones).

However, as @Bittercoder notes in Why does Castle Windsor hold onto transient objects?, you can change the release tracking policy. It seems that choosing

LifecycledComponentsReleasePolicy:

var policy = container.Kernel.ReleasePolicy;
container.Kernel.ReleasePolicy = LifecycledComponentsReleasePolicy;

But since the question was asked, that appears to have become the default policy.

如果没有 2024-07-21 05:31:11

需要注意的一件事是,这似乎已在城堡主干中修复。 在 r5475 中,Hammett 将 MicroKernel 中的默认发布策略更改为 LifecycledComponentsReleasePolicy。

One thing to note is that this seems to have been fixed in the Castle Trunk. In r5475, Hammett changed the default release policy in MicroKernel to LifecycledComponentsReleasePolicy.

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