【问题标题】:Previous version of assembly loaded even when "Specific version" is true即使“特定版本”为真,也会加载以前版本的程序集
【发布时间】:2012-11-29 08:49:27
【问题描述】:

我正在为应用程序开发插件。我的插件使用 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.

【问题讨论】:

    标签: .net gac .net-assembly


    【解决方案1】:

    我仍然不知道为什么会遇到这个问题,但我找到了解决方法。通过连接到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;
    }
    

    【讨论】:

    • 您正在创建自己的 DLL Hell 版本。特别是 LoadFile() 是超级危险的。也是您首先遇到此问题的可能原因,听起来您也在使用它来加载插件。始终使用 LoadFrom,从不 LoadFile。这就是 GAC 存在的其他原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 2011-03-18
    • 2014-12-03
    • 1970-01-01
    • 2015-04-10
    相关资源
    最近更新 更多