基于 MEF 的应用程序在本地计算机上运行良好,但从网络共享运行时不会导入插件

发布于 2024-10-25 19:28:53 字数 1435 浏览 4 评论 0原文

我正在将自行开发的插件架构转换为 .NET 4.0 的 MEF。当从我的本地计算机运行时,基于 MEF 的应用程序运行得非常好。但是,当我将应用程序移动到网络共享然后运行该应用程序时,MEF 不再加载我的插件。

当我将调试会话附加到进程并检查 DirectoryCatalog 对象时,我发现组件或零件属性中没有任何项目。 FullPath 是正确的目录,LoadFiles 属性包括正在搜索的目录中的所有 DLL。

起初我以为这是 CasPol 问题,直到我尝试修改 CasPol 并收到一条警告,指出 .NET 4.0 中默认情况下不再启用 CasPol。一定是别的东西。我对相关目录拥有完全权限。

这是将包含导入的 AddIn 的属性:

[ImportMany]
private IEnumerable<IRecipient> RecipientAddIns;

这是发现和导入 AddIn 的方法:

private void LoadRecipientAddIns()
{
  using (var catalog = new AggregateCatalog())
  {
    // Look for IRecipient AddIns in the ./Recipients directory.
    catalog.Catalogs.Add(new DirectoryCatalog("Recipients"));

    // Look for IRecipient AddIns in subdirectories hanging off of ./Recipients.
    foreach (string currentDirPath in Directory.GetDirectories("Recipients"))
      catalog.Catalogs.Add(new DirectoryCatalog(currentDirPath));

    using (var compositionContainer = new CompositionContainer(catalog))
    {
      compositionContainer.ComposeParts();
      compositionContainer.SatisfyImportsOnce(this);

      // The discovered AddIns should now be in the RecipientAddIns property.
    }

    // Do stuff with the Recipient AddIns
    foreach (var recipient in this.RecipientAddIns)
    {
      ...
    }
  }

  // Clear the list of discovered Recipient AddIns
  this.RecipientAddIns = null;
}

有什么想法吗?

谢谢。

I am converting a home-grown plugin architecture to .NET 4.0's MEF. The MEF-based application works wonderfully when it is run from my local machine. When I move the application to network share and then run the application, however, MEF no longer loads my plugins.

When I attach a debugging session to the process and inspect the DirectoryCatalog object I see there are no items in the Assemblies or Parts properties. The FullPath is the correct directory and the LoadFiles property includes all of the DLLs in the directory being searched.

At first I thought this was a CasPol issue until I tried to modify the CasPol and received a warning saying that CasPol is no longer enabled by default in .NET 4.0. It must be something else. I have Full permissions to the directory in question.

Here is the property which will contain the imported AddIns:

[ImportMany]
private IEnumerable<IRecipient> RecipientAddIns;

And here is the method that discovers and imports the AddIns:

private void LoadRecipientAddIns()
{
  using (var catalog = new AggregateCatalog())
  {
    // Look for IRecipient AddIns in the ./Recipients directory.
    catalog.Catalogs.Add(new DirectoryCatalog("Recipients"));

    // Look for IRecipient AddIns in subdirectories hanging off of ./Recipients.
    foreach (string currentDirPath in Directory.GetDirectories("Recipients"))
      catalog.Catalogs.Add(new DirectoryCatalog(currentDirPath));

    using (var compositionContainer = new CompositionContainer(catalog))
    {
      compositionContainer.ComposeParts();
      compositionContainer.SatisfyImportsOnce(this);

      // The discovered AddIns should now be in the RecipientAddIns property.
    }

    // Do stuff with the Recipient AddIns
    foreach (var recipient in this.RecipientAddIns)
    {
      ...
    }
  }

  // Clear the list of discovered Recipient AddIns
  this.RecipientAddIns = null;
}

Any ideas?

Thank you.

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

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

发布评论

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

评论(1

丑疤怪 2024-11-01 19:28:53

尝试自己加载程序集(即使用 Assembly.Load 或 LoadFrom)。我怀疑您会遇到一个异常,DirectoryCatalog 可能会吞掉该异常。异常应该可以帮助您找出问题所在。

Try loading the assemblies yourself (ie with Assembly.Load or LoadFrom). I suspect you'll get an exception, which the DirectoryCatalog is probably swallowing. The exception should help you figure out what's wrong.

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