即使“特定版本”为真,也会加载以前版本的程序集

时间:2012-11-29 08:49:28

标签: .net gac .net-assembly

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

主机应用程序使用以前版本的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.

1 个答案:

答案 0 :(得分: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;
}