如何从应用程序的 MEF 容器中取消包含字段导入的对象的链接?

发布于 2024-10-13 03:25:59 字数 1595 浏览 3 评论 0原文

我正在编写一个在 .Net 4 中使用 MEF 的 WPF 应用程序。我的应用程序有一个 CompositionContainer,我的所有视图模型都可以访问该容器,以便访问(使用 MEF)一些负责数据检索和存储的共享对象。

我一直在使用内存分析器来查看视图模型对象和数据访问对象中的一些的生命周期,以查看何时对所有内容进行垃圾收集。令我惊讶的是,我发现我的应用程序的 CompositionContainer 在视图模型被释放后仍保留对它们的引用。

下面是我尝试大致展示我如何使用 MEF。我希望有人能告诉我我做错了什么。

App.xaml.cs 中的代码

公共部分类App:应用程序
{
    私有静态 CompositionContainer _container;
    内部静态 CompositionContainer Container { get { return _container; } }

    私有无效OnStartup(对象发送者,StartupEventArgs e)
    {
        AssemblyCatalog 目录 = new AssemblyCatalog(Assembly.GetExecutingAssembly());
        _container = new CompositionContainer(目录);
    }
}

合约接口

公共接口ICostCentreService:IBaseEntityService { ... }

实现接口的导出

[PartCreationPolicy(CreationPolicy.Shared)] [导出(类型(ICostCentreService))] 公共类 CostCentreService :BaseEntityService、ICostCentreService { ... }

我的视图模型

公共类 CostCentreViewModel:ViewModelBase { [进口] 私有ICostCentreService _costCentreService;

 公共 CostCentreViewModel()
   {
       App.Container.ComposeParts(this);
   }

}

上面的代码能够提取足够多的图片来展示我如何使用 MEF。一旦我使用完视图模型并调用 Dispose 并删除对它的所有引用,就会出现问题,它不会被垃圾收集,因为应用程序的容器仍然引用它。 (在我目前屏幕上的情况下,内存分析器表示我的视图模型仍然由 App._containner._partExportProvider._parts._items[0]._cashedIntance 引用)。

所以我想知道如何摆脱这个参考。是我没有正确使用 MEF 吗?

任何帮助将不胜感激。

干杯,

尼克·巴雷特

I am writing a WPF application that uses MEF in .Net 4. My app has a CompositionContainer that is accessed by all my view models in order to get access to (using MEF) some shared objects that are responsible for data retrieval and storage.

I have been using a memory profiler to look at the lifetime of some of both my view model objects as well as the data access objects to see when everything is being garbage collected. To my surprise, I found that my app's CompositionContainer was keeping a reference to my view models after they had already been disposed.

The following is my attempt to show roughly how I'm using MEF. I'm hoping someone can show me how I'm doing it wrong.

Code in App.xaml.cs

public partial class App : Application
{
    private static CompositionContainer _container;
    internal static CompositionContainer Container { get { return _container; } }

    private void OnStartup(object sender, StartupEventArgs e)
    {
        AssemblyCatalog catalog = new  AssemblyCatalog(Assembly.GetExecutingAssembly());
        _container = new CompositionContainer(catalog);
    }
}

Contract interface

public interface ICostCentreService : IBaseEntityService
{
...
}

Exported class that implements interface

[PartCreationPolicy(CreationPolicy.Shared)]
[Export(typeof(ICostCentreService))]
public class CostCentreService : BaseEntityService, ICostCentreService
{
...
}

My view model class

public class CostCentreViewModel: ViewModelBase
{
[Import]
private ICostCentreService _costCentreService;

   public CostCentreViewModel()
   {
       App.Container.ComposeParts(this);
   }

}

Hopefully the code extracts above paint enough of a picture as to show how I am using MEF. The problem occurs once I have finished using the view model and I call Dispose and remove all references to it, it doesn't get garbage collected because the app's container still references it. (In the case I have on my screen at the moment, the memory profiler says my view model is still referenced by App._containner._partExportProvider._parts._items[0]._cashedIntance).

So I'm wondering how I get rid of this reference. Am I not using MEF properlly?

Any help would be much appreciated.

Cheers,

Nick Barrett

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

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

发布评论

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

评论(1

奢欲 2024-10-20 03:25:59

在容器被处理之前,共享部件不会被释放。共享部分意味着每个容器只能创建一个。 MEF 容器保留对它的引用,因为如果再次请求该部分,它应该返回已经创建的部分,而不是创建一个全新的部分。

对于非共享部件,您可以通过多种方式让 MEF 释放对它们的引用。

Shared parts won't be released until the container is disposed. A shared part means that only one will be created per container. The MEF container keeps a reference to it because if it is ever asked for that part again, it should return the one that was already created instead of creating a brand new one.

For NonShared parts, there are ways you can have MEF release the references to them.

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