MEF保持了非共享IDisposable的部分引用,而不是让他们通过GC收集而不是、部分、MEF、IDisposable

2023-09-02 11:52:07 作者:旧街凉风

我有点在MEF的零件寿命的问题,这会导致内存泄漏我的棱镜应用中遇到的。

I've encountered somewhat of a problem in MEF's part lifetime which causes memory leaks in my Prism application.

我的应用程序产品出口的意见和的ViewModels与 PartCreationPolicy 被设置为 CreationPolicy.NonShared 。的观点和的ViewModels从 ViewBase ViewModelBase 分别,它实现的IDisposable 。

My application exports views and viewmodels with the PartCreationPolicy being set to CreationPolicy.NonShared. The views and viewmodels inherit from ViewBase and ViewModelBase respectively, which implements IDisposable.

现在,因为我的部分实施的IDisposable ,对它们的引用保持的容器,这会导致他们不被垃圾收集器释放。据对部分寿命 MEF的文件,这是由设计:

Now, since my parts implement IDisposable, a reference to them is kept by the container, which causes them to not be released by the garbage collector. According to MEF documentation on part lifetime, this is by design:

容器不会持有引用部分它会创建除非下列条件之一为真:

The container will not hold references to parts it creates unless one of the following is true:   在该部分被标记为共享   部分农具的IDisposable   在一个或更多的进口被配置为允许重构    The part is marked as Shared The part implements IDisposable One or more imports is configured to allow recomposition

我怎样才能使MEF跟不上的引用,这些部件?是否有一个属性,我可以用它来让MEF知道,我不希望它保持一个参考我的一部分,即使它实现的IDisposable

How then can I make MEF not keep a reference to these parts? Is there an attribute I can use to let MEF know I don't want it to keep a reference to my part even if it implements IDisposable?

无论是在上面的文章中讨论的策略似乎并不像对我很好的解决方案:

Both of the strategies discussed in the above article don't seem like good solutions for me:

ReleaseExport 需要导出对象作为参数,我不知道如何来提供。我有我的看法的情况下,但没有办法,我知道什么是用于创建视图的合同。它会一直巨大的,如果有一个过载 ReleaseExport 它可以接收由容器创建的任何对象。 使用子容器似乎并不像一个自然的选择,或。 ReleaseExport requires an Export object as a parameter, which I don't know how to provide. I have the instance of my view, but there's no way for me to know what was the contract that was used to create the view. It would've been great if there was an overload for ReleaseExport which could receive any object created by the container. Using a child container doesn't seem like a natural option either.

任何帮助将大大AP preciated。

Any help will be greatly appreciated.

推荐答案

除非棱镜支持某种形式的生命期视图对象中,有没有办法解决这里除了删除的IDisposable 从被认为暴露的接口列表中。

Unless Prism supports some kind of lifetime for view objects, there is no solution here except to remove IDisposable from the list of interfaces exposed by the view.

有三种MEF方法来处理这​​个问题,所有提到的其他反应:

There are three MEF approaches to handling this problem, all mentioned by other responders:

ExportFactory&LT; T&GT; 子容器 ReleaseExport() ExportFactory<T> Child containers ReleaseExport()

所有这些都需要对的code请求原出口部分的一些工作 - 在内部棱镜这种情况下,code。这有一定道理,因为这是不可取的code消费的对象必须知道如何以及在创建时的。

All of them require some work on the part of the code that requests the original export - in this case code within Prism. This makes some sense, as it is undesirable for code consuming an object to have to be aware of how and when it was created.

没有 ReleaseExportedObject()在MEF,因为多(如房地产)的出口可以返回相同的值;它可以在逻辑上可以提供,但增加的复杂性使得它不太可能由MEF在可预见的将来处理。

There is no ReleaseExportedObject() in MEF because multiple (e.g. property) exports can return the same value; it may be logically possible to provide but the added complexity makes it unlikely to be addressed by MEF in the foreseeable future.

希望这有助于;我已经重新标记这个问题棱镜因为我相信其他人在棱镜社会将曾经遇到过这一点,并能够提供咨询。

Hope this helps; I've retagged this question 'prism' as I'm sure others in the Prism community will have encountered this and be able to offer advice.