.net 应用程序试图加载错误版本的程序集

.net 应用程序试图加载错误版本的程序集,.net,assemblies,version,.net,Assemblies,Version,我使用的是一对未签名的第三方程序集库,其中包含windows控件和基类型,我从中创建派生类。DLL位于安装我的应用程序的子文件夹中。在我无法控制的情况下,与我的可执行文件安装在同一目录中的库的更新版本具有相同的名称,但是根据.Net运行时的更高版本构建的 这会导致我的应用程序抛出一个BadImageFormatException,表示: 无法加载文件或程序集“sharedlibA”[…]此程序集无效 由比当前加载的运行时更新的运行时生成,并且无法 被加载 <runtime> &l

我使用的是一对未签名的第三方程序集库,其中包含windows控件和基类型,我从中创建派生类。DLL位于安装我的应用程序的子文件夹中。在我无法控制的情况下,与我的可执行文件安装在同一目录中的库的更新版本具有相同的名称,但是根据.Net运行时的更高版本构建的

这会导致我的应用程序抛出一个BadImageFormatException,表示:

无法加载文件或程序集“sharedlibA”[…]此程序集无效 由比当前加载的运行时更新的运行时生成,并且无法 被加载

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="sharedlibA" culture="neutral" publicKeyToken="..." />
      <codeBase version="1.0.0.0" href="bin\sharedlibA.dll" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="sharedlibB" culture="neutral" publicKeyToken="..." />
      <codeBase version="1.0.0.0" href="bin\sharedlibB.dll" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
下面是我正在使用的文件系统结构:

[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


我已经解决了我的具体问题。回答如下。

这确实应该用“停止那样做!”来解决。一个非常具体的解决方法是去掉.exe.config文件中的
元素,这样CLR就再也找不到DLL了。并实现AppDomain.CurrentDomain.AssemblyResolve事件以返回祝福者。

我通过向应用程序配置文件添加
元素来解决此问题,以指定每个dll的确切位置。显然,这是有效的,因为每次需要加载程序集时,
都会在探测启发式启动之前进行检查

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="sharedlibA" culture="neutral" publicKeyToken="..." />
      <codeBase version="1.0.0.0" href="bin\sharedlibA.dll" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="sharedlibB" culture="neutral" publicKeyToken="..." />
      <codeBase version="1.0.0.0" href="bin\sharedlibB.dll" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

噢,对了,它仍然可以找到DLL,因为它与EXE位于同一目录中。好吧,你只能说“别那么做!”