Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newtonsoft.Json Assembly Conflict

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?

like image 471
Hiren Avatar asked Jul 21 '13 19:07

Hiren


2 Answers

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> 
like image 65
Harold Avatar answered Oct 10 '22 21:10

Harold


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


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: sn command response As you can see, the Newtonsoft public key (30ad4fe6b2a6aeed) is located right there at the end.
like image 38
Ross Brasseaux Avatar answered Oct 10 '22 22:10

Ross Brasseaux