我正在使用一对未签名的第 3 方程序集库,其中包含我创建派生类的 Windows 控件和基本类型。dll 位于安装我的应用程序的子文件夹中。在我的控制之外,具有相同名称但针对更高版本的 .Net 运行时构建的新版本库安装在与我的可执行文件相同的目录中。
这会导致我的应用程序抛出 BadImageFormatException 说:
无法加载文件或程序集“sharedlibA”[...] 此程序集由比当前加载的运行时更新的运行时构建,无法加载。
这是我正在使用的文件系统结构:
[MyappFolder]
|--Myapp.exe
|--sharedlibA.dll (wrong version)
|--sharedlibB.dll (wrong version)
|--bin
|--sharedlibA.dll (need to use this)
|--sharedlibB.dll (need to use this)
我一直在阅读有关不同的程序集加载上下文并创建单独的 AppDomain 的信息,但这些似乎都不起作用。
编辑
我已经按照 Hans 的建议从配置文件中删除了探测信息,并订阅了 AssemblyResolve 事件,所以现在所有其他依赖程序集都会触发该事件,除了 sharedlibA 和 sharedlibB。它们仍然会导致 BadImageFormatException。尽管发生了变化,但 AppBase 文件夹似乎仍在被探测。
这是融合日志:
=== Pre-bind state information ===
LOG: User = ...
LOG: DisplayName = sharedlibA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=643d95c7ce242296
(Fully-specified)
LOG: Appbase = file:///C:/[MyappFolder]
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\[MyappFolder]\Myapp.exe.Config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Post-policy reference: sharedlibA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=643d95c7ce242296
LOG: Attempting download of new URL file:///C:/[MyappFolder]/sharedlibA.DLL.
ERR: Failed to complete setup of assembly (hr = 0x8013101b). Probing terminated.
编辑#2
我已经解决了我的具体问题。下面回答。