LoadFrom因FileNotFoundException而失败

时间:2014-04-08 11:43:02

标签: c# .net reflection

我的问题似乎很简单,但我找不到解决方案。

我有一个主机应用程序,它托管来自第三方库的用户控件,我称之为插件。每个插件都有它的文件夹,所有插件文件都在其中,我使用LoadFrom来加载主程序集。我的插件必须实现接口,如下所示:

public interface IPlugin : IServiceProvider, IDisposable
{
    FrameworkElement CreateControl();
}

我从“main”插件库加载此接口的具体实现。问题是,当我尝试调用方法CreateControl()时,我捕获了异常

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: Could not load file or assembly 'Xceed.Wpf.Toolkit, PublicKeyToken=3e4669d2f30244f4' or one of its dependencies. Could not find specified file.

表示依赖关系未通过loadfrom context加载。在FusionLog中我可以看到,反射搜索到处都是插件文件夹,对我来说这看起来很奇怪。我该如何处理这个问题?

加载“main”类型的代码如下所示:

AssemblyName assemblyName = AssemblyName.GetAssemblyName(fileToLoad);
var assembly = Assembly.LoadFrom(fileToLoad);
var type = assembly.GetType(typeName);

if (type == null) 
    throw new InvalidOperationException("Could not find type " + typeName + " in assembly " + fileToLoad);

var defaultConstructor = type.GetConstructor(new Type[0]);
if (defaultConstructor == null)
{
    var message = String.Format("Cannot create an instance of {0}. Either a public default constructor, or a public constructor taking IWpfHost must be defined", typeName);
    throw new InvalidOperationException(message);
}

return defaultConstructor.Invoke(null);

这是InnerException FusionLog:

    === Pre-bind state information ===
LOG: DisplayName = Xceed.Wpf.Toolkit, PublicKeyToken=3e4669d2f30244f4
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Xceed.Wpf.Toolkit, PublicKeyToken=3e4669d2f30244f4 | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\voskresenskiy\Documents\Visual Studio 2013\Projects\UIContainerIT\bin\Debug\Liga Studio.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/Xceed.Wpf.Toolkit.DLL.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.DLL.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/lib/Xceed.Wpf.Toolkit.DLL.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/lib/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.DLL.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/Xceed.Wpf.Toolkit.EXE.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.EXE.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/lib/Xceed.Wpf.Toolkit.EXE.
LOG: Attempting download of new URL file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/lib/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.EXE.

从日志反射中可以看到搜索lib文件夹(我已经在app.config中指定了探测属性,如下所示:<probing privatePath="lib" />)并且不在file:///C:/Users/voskresenskiy/Documents/Visual Studio 2013/Projects/UIContainerIT/bin/Debug/plugin/PluginName/Xceed.Wpf.Toolkit/Xceed.Wpf.Toolkit.EXE中搜索

我会感激任何帮助!

1 个答案:

答案 0 :(得分:2)

使用assembly binding - probing options

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <probing privatePath="plugins" />
    </assemblyBinding>
</runtime>

如果每个插件都有一个目录,那么您可以在探测元素中添加;分隔的所有目录,如plugin1;plugin2;...pluginX;