大会不匹配,尽管有装配重定向和加载正确的版本重定向、不匹配、加载、正确

2023-09-04 02:41:45 作者:何须执手问年华

我的控制台应用程序使用System.Net.Http.Formatting v5.1.0.0取决于Newtonsoft.Json v4.5.0.0。然而,我的应用程序包括Newtonsoft.Json的v6.0.0.0(其他原因)。

My console app uses System.Net.Http.Formatting v5.1.0.0 that depends on Newtonsoft.Json v4.5.0.0. My app however includes v6.0.0.0 of Newtonsoft.Json (for other reasons).

若要System.Net.Http.Formatting使用我增加了一个集新Newtonsoft.Json版本重定向到App.config中:

To make System.Net.Http.Formatting use the new Newtonsoft.Json version I added an assembly redirect to the 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>

不过,我得到以下异常:

Still I get the following exception:

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)

融合日志显示正确的程序集加载,但失败,因为不匹配的:

The fusion log shows that the correct assembly is loaded but it fails because of mismatch:

*** 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.

我能做些什么来解决这个不匹配?先谢谢了。

What can I do to resolve this mismatch? Thanks in advance.

解决方案

有什么不妥的重定向。唯一的线索是,它在某种程度上甚至没有得到应用,虽然看似一切都如预期(请注意,即使日志显示,加载了正确的配置文件)。问题是,有这个声明在另一个装配问题assemblyBinding部分:

There was nothing wrong with the redirect. The only clue was that it somehow didn't even get applied, although seemingly everything worked as expected (note that the log even shows that the correct config file is loaded). The issue was that there was this declaration in the assemblyBinding section for another assembly issue:

<qualifyAssembly partialName="log4net" fullName="log4net, 1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" />

这行解决了,其他问题,但不知何故,打破了JSON的重定向。我不知道为什么:该qualifyAssembly声明是正确的推测过于

This line solved that other issue but somehow broke the Json redirect. I don't know why: the qualifyAssembly declaration is supposedly correct too.

不过除去这个声明作出的装配重定向工作...

Nevertheless removing this declaration made the assembly redirect work...

推荐答案

目前没有证据证明你的&LT; bindingRedirect&GT; 生效。你应该看到:

There is no evidence that your <bindingRedirect> is in effect. You should see:

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

有你的问题没有面包,但无论你编辑看来不会是D:\ ConsoleApplication1 \斌\调试\ ConsoleApplication1.exe.Config。奇盘符。当心的项目已经有一个app.config工程项目,并在添加App1.config。类似的东西。

There are no breadcrumbs in your question, but whatever you edited appeared to not have been D:\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe.Config. Odd drive letter. Beware of a project already having an App.config project item and you adding App1.config. Something like that.