Newtonsoft.Json Assembly Conflict

橙三吉。 提交于 2019-12-28 01:56:27

问题


I use Netonsoft.Json in my project. It works fine until I start integrating Paypal SDK in my Project. My code is as below.

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

and I get error as below

       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)

I search on internet and found some solution to change config file. SO I change my config file as below

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

I also play around with assembly properties like Copy Local, Specific Version but nothing helps me to solve this. How Can I solve assembly conflict?


回答1:


I just had the same problem and I solved it by updating the Newtonsoft.Json to the latest version using

Update-Package Newtonsoft.Json

and then going to Web.config and adding:

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



回答2:


+1 to zbarrier for his solution. Here's why it worked...

+1 to zbarrier for his answer which helped me solve my issue. Assembly reference issues are the worst...so I thought I would post the steps I took, as well as some things I learned, and hopefully it helps:


  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:

    Then, I double-clicked on the 1 [Neutral] icon which brought me to the assembly's information, seen here:

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


Other notes in case anyone is still confused:

  • the <runtime> tag goes within the <configuration></configuration> tag in the web.config. The section I show above was pasted directly below the opening tag of my web.config's <configuration> section.
  • the xmlns attribute represents the corresponding XML namespace. This is used by the developers of the assembly to avoid issues with conflicting tags. In this case you should feel safe using the xmlns="schemas-microsoft-com:asm.v1" listed above.
  • you can alter the oldVersion attribute to forward additional versions of the assembly. For example, I will probably edit mine to look more like zbarrier's answer.
  • the publicKeyToken is another attribute that pretty much stays the same when it comes to Newtonsoft.Json. The publicKeyToken is just a shortened version of the public key--much like a title is to a book--and in this case doesn't really change. If you ever want to know the public key to an assembly, just open the Developer Command Prompt which can be found in the start menu, then use the command prompt to navigate to the location of the assembly file (in this case ~\Bin\), and run the sn -T assembly_file_name command. So in this case, the command was sn -T Newtonsoft.Json.dll. You should get a response like this:

    As you can see, the Newtonsoft public key (30ad4fe6b2a6aeed) is located right there at the end.



回答3:


I ran into the same problem for assembly version 6.0.1. I pasted the following lines into the web.config as directed by Harold:

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

I then removed the project reference to Newtonsoft.Json and deleted the reference to Newtonsoft.Json in the packages.config file.

I opened the Nuget Manager and reinstalled Newtonsoft.Json.

The install changed the web.config settings to the following and everything worked fine:

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



回答4:


At first , i thought my case was the same old assembly reference ... did the uninstall, reinstall, force install, rebuild, add redirect assembly ...etc

Nothing worked until, I found out that another assembly was causing the problem.

In my case, my code was failing when I called the HttpClient.PostAsJsonAsync(requestURI, T ) method. The error about Assembly Reference threw me off since my Solution has multiple projects and in which some of the projects used an old version... ended wasting a lot of time until...

My solution:

  • Removed the existing System.Net.Http.Formatting from my References
  • Installed the Install-Package Microsoft.AspNet.WebApi.Client - which installed the required Http.Formatting.

Once installed, the PostAsJsonAsync() worked as expected!

Hope this saves someone time I've lost looking for a solution!




回答5:


I faced the same problem, I have installed Newtonsoft.Json v9.0.1 , sandcastle stops the build displaying the same error but with version difference: "Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0,"

what worked: find/create a project with newtonsoft.json with the version SandCastle is asking for, add the file "Newtonsoft.Json.dll" as a reference to the SC project then build. (you can find the dll in the bin folder of the project)



来源:https://stackoverflow.com/questions/17776090/newtonsoft-json-assembly-conflict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!