我得到了错误

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>

它也没有起作用


当前回答

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

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

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

其他回答

我也有同样的问题,当我试图创建MassTransit队列时,我得到了异常:

"Exception: System.TypeInitializationException: The type initializer for 'MassTransit.Serialization.JsonMessageSerializer' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.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)"

对我有效的解决方案(在花了几天时间恢复了几次提交后):

We had a windows service solution that has .Service project and .XUnitTests project. Both of them were using a common nuget that has dependency on Newtonsoft.Json.dll. There was no explicit reference to Newtonsoft.Json nuget package in both projects (but we were using 'using Newtonsoft.Json;' namespace in our classes), so the common nuget was using version 9 of Newtonsoft.Json by default. As soon as I installed the Newtonsoft.Json nuget in both .Service and .XUnitTests projects, the common nuget package started using the latest v12 Newtonsoft and that fixed my issue.

如果能节省大家宝贵的时间,就贴在这里吧。

移除牛顿软。Json程序集从项目引用并再次添加它。您可能不小心删除或替换了dll。

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

为我工作....只需将你正在使用的版本放在newVersion中,即(newVersion="7.0.0.0")

将正确的版本部署到CI机器

这是在告诉你,程序集加载器找到了一个不同版本的Newtonsoft。Json程序集,它与您在项目中创建的引用不匹配。要正确加载程序集,必须将程序集与已编译的代码并排部署,或者在目标计算机(即GAC)中安装程序集的正确版本。

替代方案:确保配置在正确的文件中

如果要保留当前解决方案,并加载具有不同版本的程序集,请确保您发布的配置位于正确的.config文件中。请记住,没有xpto.dll。config,应用程序加载的DLL总是使用运行应用程序的配置文件。

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

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

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