33

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?

Luke
  • 22,826
  • 31
  • 110
  • 193
Hiren
  • 1,381
  • 5
  • 24
  • 41

5 Answers5

62

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>
nramirez
  • 5,230
  • 2
  • 23
  • 39
Harold
  • 748
  • 7
  • 8
  • 1
    Adding that section to the web.config fixed the problem for me. This error started occurring out of the blue when I updated a bunch of NuGet packages all at once today. It was failing when I launched my MVC web app, in my global.asax on this line: GlobalConfiguration.Configure(WebApiConfig.Register); I have no idea why my web.config did NOT have this required configuration before.... or why it stopped working when I updated to latest MVC / WebAPI / JSON.NET nuget packages..... – Jason Parker Feb 06 '14 at 20:31
  • I update has this says, but MVC 5 has in the web config: why does not update to this version? – Diego Unanue Mar 22 '15 at 22:30
  • i had the issue when i installed `Microsoft.Owin.Security.Facebook` then automatically `Newtonsoft.Json` was installed and version changed from 4.5.0.0 to 6.0.0.0 and then [this](http://westdiscgolf.blogspot.com/2014/02/updated-jsonnet-and-now-owin-errors.html) post helped , these recent update really effects the app, why they don't fix it – Shaiju T Dec 13 '15 at 11:55
35

+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.
Community
  • 1
  • 1
Ross Brasseaux
  • 3,879
  • 1
  • 28
  • 48
  • Thanks. The multiple other answers on this topic don't make it clear that the major Assembly Version needs to be referenced. Banged away at this for awhile before finding this. – Brad Patton Nov 03 '15 at 22:02
  • How exactly did you opetn newtonsoft.json.dll with visual studio? – tkit Mar 24 '16 at 13:19
  • The exact steps for that would depend on the type of project with which you are working, but basically you open it like you would any other file: find the file named `Newtonsoft.Json.dll` in the solution explorer and double-click it. The only complications are that it may be hidden, so you'll have to click the "Show All Files" icon at the top of the solution explorer. For most projects, `Newtonsoft.Json.dll` can be found inside the "~\Bin\" directory after you install the Newtonsoft package from Nuget. Alternatively, you can add the file to your project using "Add" --> "Existing Item". – Ross Brasseaux Mar 24 '16 at 20:25
  • Thanks. Saves my night! – Rajaram Shelar Aug 30 '16 at 16:27
23

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>
zbarrier
  • 231
  • 2
  • 6
3

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!

Gotcha
  • 1,039
  • 1
  • 16
  • 24
0

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)

Apex ND
  • 440
  • 5
  • 12