尽管具有程序集重定向和加载正确的版本,但程序集不匹配

Pie*_*one 10 .net .net-assembly

我的控制台应用程序使用依赖于Newtonsoft.Json v4.5.0.0的System.Net.Http.Formatting v5.1.0.0.我的应用程序包括Newtonoft.Json的v6.0.0.0(由于其他原因).

为了使System.Net.Http.Formatting使用新的Newtonsoft.Json版本,我添加了一个程序集重定向到App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我仍然得到以下异常:

A first chance exception of type 'System.IO.FileLoadException' occurred in System.Net.Http.Formatting.dll

Additional information: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Run Code Online (Sandbox Code Playgroud)

融合日志显示正确的程序集已加载但由于不匹配而失败:

*** Assembly Binder Log Entry  (2014.08.10. @ 13:13:25) ***

The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  D:\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
 (Fully-specified)
LOG: Appbase = file:///D:/ConsoleApplication1/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = ConsoleApplication1.exe
Calling assembly : System.Net.Http.Formatting, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///D:/ConsoleApplication1/bin/Debug/Newtonsoft.Json.DLL.
LOG: Assembly download was successful. Attempting setup of file: D:\ConsoleApplication1\bin\Debug\Newtonsoft.Json.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Run-from-source setup phase failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能解决这种不匹配问题?提前致谢.

重定向没有任何问题.唯一的线索是它以某种方式甚至没有应用,虽然看似一切都按预期工作(请注意,日志甚至显示正确的配置文件已加载).问题是在assemblyBinding部分中有另一个程序集问题的声明:

<qualifyAssembly partialName="log4net" fullName="log4net, 1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" />
Run Code Online (Sandbox Code Playgroud)

这条线解决了其他问题,但不知何故打破了Json重定向.我不知道为什么:qualifyAssembly声明也应该是正确的.

尽管如此,删除此声明使得程序集重定向工作......

Han*_*ant 6

没有证据证明您<bindingRedirect>的生效.你应该看到:

LOG: Redirect found in application configuration file: 4.5.0.0 redirected to 6.0.0.0.
LOG: Post-policy reference: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
Run Code Online (Sandbox Code Playgroud)

您的问题中没有面包屑,但无论您编辑的内容似乎都不是D:\ ConsoleApplication1\bin\Debug\ConsoleApplication1.exe.Config.奇怪的驱动器号.注意已经有App.config项目项目并添加App1.config的项目.这样的事情.


sil*_*jon 5

只是想在此处添加少量其他细节,因为这是一个特别令人讨厌的问题,大蒜,银子弹和圣水都供不应求。

我们最近遇到了这个问题,发现原因是由于不良的自动合并而导致app.config中的XML违反架构,例如

<dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
        <assemblyIdentity name="Microsoft.Extensions.FileProviders.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

我们没有注意到文件中的问题,因为它在技术上仍然是有效的XML,并且Visual Studio解析器从不抱怨。

但是,运行时绑定例程拒绝应用任何绑定重定向(甚至在格式错误的部分之外说明的重定向)。此外,如上所述,在融合日志中没有迹象表明运行时无法解析配置文件。它可以看到正确的dll和配置文件位置,但仍会因版本差异较小而失败,就好像从未应用任何重定向一样。

TL; DR-如果您遇到这种情况,请检查您的配置文件XML是否100%完美-并且不要相信VS中的弯曲行将揭示所有可能的问题。