0

我正在开发一个应用程序的插件。我的插件使用 WPF Toolkit 1.6.0.0。WPF Toolkit 程序集是强命名的,我确保我对程序集的所有引用都是特定版本的。

宿主应用程序使用以前版本的 WPF Toolkit (1.5.0.0)。当我的插件尝试加载 WPFToolkit 时,它会从主机应用程序加载版本,即使正确的程序集与我的插件程序集位于同一文件夹中(我从该文件夹加载其他依赖项没有问题)。

如何确保加载了正确版本的库?

下面是装配活页夹日志。

*** Assembly Binder Log Entry  (29.11.2012 @ 09:35:17) ***

The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\Path\To\Host\Application\Application.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = XXXXXXXXXXXXX
LOG: DisplayName = WPFToolkit.Extended, Version=1.6.0.0, Culture=neutral, PublicKeyToken=3e4669d2f30244f4
 (Fully-specified)
LOG: Appbase = file:///C:/Path/To/Host/Application/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = application.exe
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:\Path\To\Host\Application\petrel.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: Post-policy reference: WPFToolkit.Extended, Version=1.6.0.0, Culture=neutral, PublicKeyToken=3e4669d2f30244f4
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Path/To/Host/Application/WPFToolkit.Extended.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Path\To\Host\Application\WPFToolkit.Extended.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: WPFToolkit.Extended, Version=1.5.0.0, Culture=neutral, PublicKeyToken=3e4669d2f30244f4
WRN: Comparing the assembly name resulted in the mismatch: Minor Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
4

1 回答 1

0

我仍然不知道为什么会遇到这个问题,但我找到了解决方法。通过连接AppDomain.CurrentDomain.AssemblyResolve并手动加载程序集,我能够正确加载程序集。

这是解析器的代码:

private static Assembly HandleAssemblyResolve(object sender, ResolveEventArgs args)
{
    Assembly requestingAssembly = (args.RequestingAssembly ?? Assembly.GetExecutingAssembly());
    AssemblyName assemblyName = new AssemblyName(args.Name);

    string currentLocation = requestingAssembly.Location;
    string baseDirectory = Path.GetDirectoryName(currentLocation);
    string assemblyLocation = Path.Combine(baseDirectory, assemblyName.Name + ".dll");

    if (File.Exists(assemblyLocation))
    {
        return Assembly.LoadFile(assemblyLocation);
    }

    return null;
}
于 2012-11-29T10:25:56.543 回答