我得到了错误

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>

它也没有起作用


当前回答

这是非常古老的,似乎仍然有许多人有同样的问题。所以我想分享我的经验,它可能会帮助别人。

我在两个地方也有同样的问题。在一个项目中使用6.0.4.0,在不同的项目中使用4.5.0.0。

1-这对我很有用。在bin文件夹中,我有6.0.0.0 Newtonsoft.Json.dll和到4.5.0.0 dll的符号链接

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<probing privatePath="bin\4.5dlls-path;" />
<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>

如果你不知道如何在这里创建符号链接。

mklink /D "文件夹名称" " dll路径"

2-在这种情况下,当我从Web配置文件中删除部分时,它工作了。记住,我在不同的项目中参考了6.0.0.0和4.5.0.0。在符号链接中,我有12.0.1.0 dll和6.0.0.0 bin。

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<probing privatePath="bin\12.0.1dlls-path;" />
</dependentAssembly>
</assemblyBinding>
</runtime>

3-我还有一个解。如果您在不同的项目中有不同版本的Newtonsoft.Json.dll,请尝试将所有版本升级到一个版本或最新版本,但在某些情况下可能无法工作。system.net.http.formating.dll可能需要netttonsoft . json .dll 6.0.0.0版本。在这种情况下,你需要6.0.0.0的版本,所以尽量让所有的版本都相同。希望这能帮助到一些人。

其他回答

通常情况下,添加绑定重定向应该可以解决这个问题,但对我来说行不通。经过几个小时的思考,我意识到在我的web.config中有一个xmlns属性导致了问题。从Web中的配置节点中删除xmlns属性后。配置时,绑定重定向按预期工作。

http://www.davepaquette.com/archive/2014/10/02/could-not-load-file-or-assembly-newtonsoft-json-version4-5-0-0.aspx

我修复了添加这个绑定重定向到我的.config文件的问题:

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

错误消息抱怨找不到版本4.5.0.0,当前版本的Newtonsoft。Json是6.0.0.0,所以重定向应该从4.5到6.0,而不是相反

我通过安装Nuget包解决了这个问题:Microsoft ASP。NET Web API 2.2客户端库。这反过来安装了newtonsoft。Json版本6.04

我也有同样的问题,当我试图创建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.

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

除了这里的许多答案之外,我发现了一些东西,因此添加了它。第一点,确保dll没有从GAC引用。除此之外,

在添加/更改Newtonsoft的dll时。Json,有时*。项目的Csproj不会被更改,否则json DLL的更改版本将不会反映在解决方案的Csproj中。

在notepad++中打开项目,搜索Newtonsoft。并显式地将Json从旧版本重命名为新版本。现在切换回VS后,解决方案/项目将重新加载所需的Newtonsoft版本。JSON dll。