【发布时间】:2014-05-21 02:03:54
【问题描述】:
我的问题似乎很简单,但我找不到解决办法。
我有一个主机应用程序,它托管来自第三方库的用户控件,我称之为插件。每个插件都有它的文件夹,所有插件文件都位于其中,我使用LoadFrom 加载主程序集。我的插件必须实现接口,如下所示:
public interface IPlugin : IServiceProvider, IDisposable
{
FrameworkElement CreateControl();
}
我从“主”插件库加载此接口的具体实现。问题是当我尝试调用方法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 上下文加载的。在 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 中搜索
如有任何帮助,我将不胜感激!
【问题讨论】:
-
您是否为项目添加了正确的引用?
标签: c# .net reflection