我得到了错误

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>

它也没有起作用


当前回答

我花了几天时间试图解决这个令人沮丧的问题。我几乎试遍了网上能找到的所有方法。最后我发现这个错误可能是由一个解决方案中不同的目标. net项目版本(4.5和4.5.1)引起的(就像我的情况一样)。下面的步骤帮我解决了这个问题:

仔细检查解决方案中每个项目的. net版本。只需右键单击项目,然后进入属性。

If possible set the same .Net version for all projects. If not at least try to change the Startup project one (for me this was the one causing the issues). Remove all Newtonsoft.Json packs from the solution. uninstall-package newtonsoft.json -force Update all Newtonsoft.Json versions in all packages.config files, like so <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net451" /> Reinstall Newtonsoft.Json from "Package Manager Console" with: install-package newtonsoft.json Rebuild the solution

(可选)7。如果更改了Startup项目,请再次返回

其他回答

致所有使用牛顿软体有问题的人。Json v4.5版本尝试在web中使用这个。Config或app.config:

<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="6.0.0.0"/>
       </dependentAssembly>
    </assemblyBinding>
</runtime>

重要提示:检查配置文件的配置标记是否没有命名空间属性(如https://stackoverflow.com/a/12011221/150370中建议的那样)。否则,assemblyBinding标记将被忽略。

关闭解决方案。

打开包。Config和*。用csproj文本编辑器删除任意一行都有Newtonsoft。Json

Ex:

<Reference Include="Newtonsoft.Json,Version=9.0.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net40\Newtonsoft.Json.dll</HintPath>
      <Private>True</Private>
</Reference>

Or

<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net40" />

再次打开解决方案并重新安装Newtonsoft。Json由安装包Newtonsoft。Json

这对我很管用。

在我的案例中,在下载程序集并将引用添加到项目后,我通过在将引用添加到项目之前“解锁”DLL来解决这个问题。

使用Windows资源管理器,浏览到DLL位置,右键单击DLL,然后选择“属性”。您将在其中一个选项卡上找到一个“unblock”按钮,然后您可以添加引用,程序集将正确加载。

我认为你指向错误的目标,把它改为4.5而不是6.0

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

这应该有用。

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

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

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

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