CompositionContainer loading wrong directory through DirectoryCatalog

こ雲淡風輕ζ 提交于 2019-12-11 09:07:23

问题


I'm running into an odd problem with MEF's composition container and directory catalog where I pass in a full path to a folder containing assemblies:

In C:\Program Files\MyCompany\MyApp\Bin

AssemblyA: ExportedTypeA, ExportedTypeB

AssemblyB: BaseExportedTypeA, BaseExportedTypeB

Executed from another location (C:\users\Me\dev\project\foo\bar\bar2\bin\debug\OtherApp.Exe)

In the application I call:

string ProviderPhysicalPath = @"C:\Program Files\MyCompany\MyApp\Bin";
using (DirectoryCatalog catalog = new DirectoryCatalog(ProviderPhysicalPath))
using (CompositionContainer container = new CompositionContainer(catalog))
{
     container.ComposeParts(this);
}

On container.ComposeParts(this) I get the following exception:

The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) Could not find a part of the path 'C:\USERS\Me\dev\project\foo\bar\bar2\BIN\DEBUG\BIN\'.

What's confusing is two parts:

  1. Why is it looking in a directory I haven't specified?
  2. And why is it appending \bin\ to the executing directory to do it?

Is there something I'm doing wrong?


回答1:


You might be running into this problem:

When using a DirectoryCatalog or passing a path to the AssemblyCatalog constructor, MEF will attempt to load assemblies in the default load context. However, if the assemblies are not in the probing path or the GAC, this will not be possible, and MEF will load them in the load-from context instead.

@"C:\Program Files\MyCompany\MyApp\Bin" might not be in the probing path. I can't say I have experienced the issue or explored it much. See Daniel Plaisted's Blog for details, scroll down to the section 'Assembly Load Issues'

How to Debug and Diagnose MEF Failures



来源:https://stackoverflow.com/questions/11164797/compositioncontainer-loading-wrong-directory-through-directorycatalog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!