2012-11-29 32 views
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. 

回答

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; 
} 
+1

你是用自己的方式來創建自己的DLL地獄的版本。特別是LoadFile()是非常危險的。另外,您首先得到此問題的可能原因聽起來像您使用它來加載插件。始終使用LoadFrom,*從不* LoadFile。這是GAC存在的原因。 –

相關問題