我得到了错误

fileloadexception:无法加载文件或程序集 “Newtonsoft。Json,版本=4.5.0.0,文化=中性, PublicKeyToken=30ad4fe6b2a6aeed'或其依赖项之一。的 定位程序集的清单定义与该程序集不匹配 参考。(异常来自HRESULT: 0x80131040)

用于我的CI构建

我尝试过的解决方案

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
        culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>

它也没有起作用


当前回答

如果您在同一解决方案中使用多个项目 和另一个的图书馆 检查是否所有项目都有相同版本的Newtonsoft。Json

其他回答

我写了一个程序来自动修复这个问题。它将确保您的程序能够使用硬盘驱动器上目标程序集的最新版本,而不仅仅是依赖库需要的版本。

https://github.com/BackTrak/DependencyFixup/releases/tag/1.0.0.0

无需再猜测,它将自动为您修复所有清单引用问题。

通过nuget重新安装newtonsoft包不适合我。 我必须手动调用JsonConvert。反序列化对象以绕过此问题

我改变了

HttpResponseMessage response = await client.GetAsync(url));
response.EnsureSuccessStatusCode();
MyObject data = await response.Content.ReadAsAsync<MyObject>();

For

HttpResponseMessage response = await client.GetAsync(url));
response.EnsureSuccessStatusCode();
string jsonStr = await response.Content.ReadAsStringAsync();
MyObject data = JsonConvert.DeserializeObject<MyObject>(jsonStr);

我在7.0.0.0版本中遇到了完全相同的问题,导致我的问题的库是Microsoft.Rest.ClientRuntime,它以某种方式引用了错误的Newtonsoft版本(6.0.0.0)。Json,尽管正确的依赖管理在nugget(正确版本的newtonsoft。Json(7.0.0.0)被安装)。

我通过在配置文件中应用上述从6.0.0.0重定向到7.0.0.0(从Kadir Can)来解决这个问题:

<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="7.0.0.0" />

---->经过几天没有改变任何东西,它又出现了同样的错误。我安装了6.0.0.0版本,更新到7.0.0.0,现在可以正常工作了。

我得到了同样的错误,并通过添加以下代码错误解决了生产。

回答这个问题已经太迟了,但也许能帮到别人。

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>

另一个潜在的问题是,如果元素在任何其他dependentAssembly元素上有不正确的配置,那么绑定重定向似乎就会无声地失败。

确保每个元素下只有一个元素。

在某些情况下,VS生成这个:

  <dependentAssembly>
    <assemblyIdentity ...
    <assemblyIdentity ...
  </dependentAssembly>

而不是

  <dependentAssembly>
    <assemblyIdentity ...
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity ...
  </dependentAssembly>

我花了很长时间才意识到这就是问题所在!