MEF ExportFactory- 如何在长时间运行的应用程序中正确处置?
基本上,是否有一种简单的方法来处理由 ExportFactory
创建的导入?我问的原因是因为导出通常包含对仍然存在的内容的引用,例如 EventAggregator。我不想遇到这样的问题:我创建了数百个这样的东西,并在不需要时将它们闲置。
我注意到,当我创建对象时,我会返回一个带有 Dispose 的 ExportLifetimeContext
。但是,我不想将 ExportLifetimeContext
传回请求 ViewModel 副本的 ViewModel,因此我传回值。 (return Factory.Single(v => v.Metadata.Name.Equals(name)).CreateExport().Value;
)
Basically, is there an easy way to dispose of the imports that are created by an ExportFactory<T>
? The reason I ask is because the exports usually contain a reference to something that is still around, such as the EventAggregator. I don't want to run into the issue where I'm creating hundreds of these and leaving them laying around when they are not necessary.
I noticed that when I create the objects I get back a ExportLifetimeContext<T>
which carries a Dispose with it. But, I don't want to pass back an ExportLifetimeContext
to my ViewModels requesting copies of the ViewModel, hence I pass back the Value. (return Factory.Single(v => v.Metadata.Name.Equals(name)).CreateExport().Value;
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您对
ExportLifetimeContext
调用Dispose
时,它将对参与创建T
的任何NonShared
部分调用 dispose代码>.它不会处置任何Shared
组件。这是一种安全的行为,因为如果NonShared
部分纯粹是为了满足T
的导入而实例化的,那么它们可以安全地被处置,因为它们不会被任何人使用其他进口。我认为您可以实现它的唯一其他方法是自定义
Dispose
方法,将 dispose 调用链接到您导入的任何其他成员属性,例如:But as您的类型不知道导入的
IBar
实例是Shared
还是NonShared
您面临处置共享组件的风险。我认为挂在
ExportedLifetimeContext
实例上是实现您想要的目标的唯一安全方法。不确定这是否有帮助,感觉像是不必要的包装,但你可能:
你可能:
感觉有点脏......
When you call
Dispose
on anExportLifetimeContext<T>
it will call dispose on anyNonShared
part involved in the creation ofT
. It won't dispose of anyShared
components. This is a safe behaviour, because if theNonShared
parts were instantiated purely to satisfy the imports forT
, then they can safely be disposed as they won't be used by any other imports.The only other way I think you could achieve it, would be to customise the
Dispose
method to chain the dispose call to any other member properties you import with, e.g.:But as your type has no visibility of whether the imported instance of
IBar
isShared
orNonShared
you run the risk of disposing of shared components.I think hanging onto the instance of
ExportedLifetimeContext<T>
is the only safe way of achieving what you want.Not sure if this helps, feels like unnecessary wrapping, but could you possibly:
Which you could possibly:
Feels a bit dirty...