Newtonsoft.Json程序集冲突

33

我在我的项目中使用了Netonsoft.Json。它一直很好用,直到我开始将Paypal SDK集成到我的项目中。我的代码如下。

         String AccessToken =
  new PayPal.OAuthTokenCredential("", "").GetAccessToken(); ---->>>> This Line Throwing An Error
            PayPal.Api.Payments.Address add = new PayPal.Api.Payments.Address();
            add.city = TextBoxCity.Text;
            add.line1 = TextBoxAddress.Text;
            add.phone = TextBoxPhoneNumber.Text;
            add.postal_code = TextBoxZipcode.Text;
            add.state = TextBoxState.Text;
            PayPal.Api.Payments.CreditCard cc = new PayPal.Api.Payments.CreditCard();
            cc.number = TextBoxCreditCardNumber.Text;
            cc.first_name = TextBoxFirstName.Text;
            cc.last_name = TextBoxLastName.Text;
            cc.expire_month = Convert.ToInt16(TextBoxExpiryMonth.Text);
            cc.expire_year = Convert.ToInt16(TextBoxExpiryYear.Text);
            cc.cvv2 = TextBoxCVVNumber.Text;
            cc.billing_address = add;
            cc.Create(AccessToken);

我收到以下错误信息:

       System.IO.FileLoadException: 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)

我在互联网上搜索并找到了一些更改配置文件的解决方案。所以我按照以下方式更改了我的配置文件:

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

我也尝试了一些汇编属性,例如Copy Local(复制本地)和Specific Version(指定版本),但没有帮助我解决这个问题。我应该如何解决程序集冲突?


1
使用NuGet包管理器中的合并选项。 - niico
5个回答

62

我之前遇到了同样的问题,通过更新Newtonsoft.Json至最新版本来解决了它。

Update-Package Newtonsoft.Json

然后前往Web.config并添加:

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

1
将该部分添加到web.config文件中,问题得到了解决。今天我一次性更新了一堆NuGet包后,这个错误突然出现了。当我启动我的MVC Web应用程序时,在global.asax文件的以下行上失败:GlobalConfiguration.Configure(WebApiConfig.Register); 我不知道为什么我的web.config文件之前没有这个必需的配置...或者为什么在我更新到最新的MVC / WebAPI / JSON.NET NuGet包时它停止工作了... - ClearCloud8
我更新了这个说法,但MVC 5在web config中有:<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /><bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />为什么不更新到这个版本? - Diego Unanue
我在安装Microsoft.Owin.Security.Facebook时遇到了问题,然后自动安装了Newtonsoft.Json,版本从4.5.0.0更改为6.0.0.0,然后这篇文章帮了我很多。这些最近的更新真的影响了应用程序,他们为什么不修复它呢? - Shaiju T

35

+1给zbarrier的解决方案。这是它成功的原因...

+1给zbarrier的答案,帮助我解决了我的问题。组件引用问题是最糟糕的...所以我想我会发布我采取的步骤,以及我学到的一些东西,希望能有所帮助:


  1. FAILED ATTEMPT: Pasted the following lines into my web.config:

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

    ^^^^^ DID NOT WORK


  1. SOLUTION: Navigated to ~/Bin/Newtonsoft.Json.dll, and opened the file in Visual Studio. By default, the interface for the file displays a folder named after the assembly--I double clicked it to expand, and eventually saw this: assembly-file interface Then, I double-clicked on the 1 [Neutral] icon which brought me to the assembly's information, seen here: assembly-file information

    The line that says Assembly Version is what you'll need to enter into the newVersion attribute of the <bindingRedirect> tag. So I took the section I pasted (in step one) and change the "5.0.8" to "6.0.0.0". My new <runtime> section looks like this:

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

    ^^^^^ IT WORKED!! Finally...


其他注意事项以防有人还是困惑:

  • <runtime> 标签应该放在 web.config 文件的 <configuration></configuration> 标签内。我展示的这个部分直接粘贴在我的 web.config 的 <configuration> 部分的开头标签下面。
  • xmlns 属性代表相应的 XML 命名空间。这是由程序集的开发者使用的,以避免与冲突标签的问题。在这种情况下,您可以放心地使用上面列出的 xmlns="schemas-microsoft-com:asm.v1"
  • 您可以更改 oldVersion 属性以转发程序集的其他版本。例如,我可能会编辑我的内容,让它看起来更像 zbarrier 的答案
  • publicKeyToken 是另一个属性,当涉及到 Newtonsoft.Json 时基本保持不变。publicKeyToken 只是公钥的缩写形式,就像标题是书籍一样,在这种情况下并没有真正改变。如果您想要知道程序集的公钥,请打开“开发人员命令提示符”,该命令提示符可以在开始菜单中找到,然后使用命令提示符导航到程序集文件的位置(在这种情况下是 ~\Bin\),并运行sn -T assembly_file_name 命令。所以在这种情况下,命令是 sn -T Newtonsoft.Json.dll。您应该会得到这样的响应:sn command response 如您所见,Newtonsoft 的公钥 (30ad4fe6b2a6aeed) 就在最后面。

谢谢。在这个主题上的多个其他答案并没有明确指出需要引用主要的程序集版本。在找到这个之前,我一直在艰苦卓绝地努力。 - Brad Patton
你是如何在Visual Studio中准确打开newtonsoft.json.dll的? - tkit
具体步骤取决于您正在使用的项目类型,但基本上,您可以像打开其他文件一样打开它:在解决方案资源管理器中找到名为“Newtonsoft.Json.dll”的文件,然后双击它。唯一的复杂之处在于它可能被隐藏,所以您需要点击解决方案资源管理器顶部的“显示所有文件”图标。对于大多数项目而言,在从Nuget安装了Newtonsoft包后,Newtonsoft.Json.dll可以在“~\Bin\”目录下找到。或者,您可以使用“添加”-->“现有项目”将该文件添加到您的项目中。 - Ross Brasseaux
谢谢。这救了我的晚上! - Rajaram Shelar

23

我遇到了与程序集版本6.0.1相关的同样问题。 我按照Harold的指示将以下代码行粘贴到了web.config文件中:

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

然后我删除了对Newtonsoft.Json的项目引用,同时在packages.config文件中删除了对Newtonsoft.Json的引用。

我打开Nuget管理器并重新安装了Newtonsoft.Json。

安装后,web.config设置变为以下内容,一切正常:

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

3
起初,我以为我的情况与老问题一样...进行了卸载、重新安装、强制安装、重新构建、添加重定向程序集等操作,但是都没有起作用。
直到我发现另一个程序集引起了问题。
在我的情况下,当我调用HttpClient.PostAsJsonAsync(requestURI, T)方法时,我的代码会失败。由于我的解决方案中有多个项目,并且其中一些项目使用了旧版本,所以关于程序集引用的错误让我感到困惑...最终浪费了很多时间,直到...
我的解决方案:
1. 从我的引用中删除现有的System.Net.Http.Formatting 2. 安装Install-Package Microsoft.AspNet.WebApi.Client - 这将安装所需的Http.Formatting。
安装完成后,PostAsJsonAsync()按预期工作!
希望这能节省某人寻找解决方案所花费的时间!

0

我遇到了相同的问题,我安装了 Newtonsoft.Json v9.0.1 ,但是 sandcastle 停止构建并显示相同的错误,只是版本不同:

解决方法:找到或创建一个带有 SandCastle 要求的 newtonsoft.json 版本的项目,在 SC 项目中将文件 "Newtonsoft.Json.dll" 添加为引用,然后进行构建。(你可以在该项目的 bin 文件夹中找到这个 dll 文件)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接