10
votes

I have a C# project that has direct reference to Newtonsoft.Json and some other references(which has indirect reference to Newtonsoft.Json). I want to upgrade Newtonsoft.Json version from 6.0.8 to the latest 12.0.2 in all the places.

Initially I got the reference assembly cannot be resolved issue but after searching I've tried put binding in app.config and the solution can be build successfully now:

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

And my project looks like this:

<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
      <SpecificVersion>False</SpecificVersion>
      <Private>True</Private>
    </Reference>

Now the issue becomes when calling the library in run time, it throws following error:

mymethod threw an exception, will try again: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified."

With using AsmSpy, it seems the indirect references is the root cause:

Reference: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed Source: NotFound 6.0.0.0 by Microsoft.Azure.KeyVault, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 6.0.0.0 by Microsoft.Azure.KeyVault.WebKey, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 6.0.0.0 by Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 6.0.0.0 by Microsoft.Rest.ClientRuntime.Azure, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

By enabling the diagnostic project output, it seems fine when building:

2>  Unified primary reference "Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed". (TaskId:19)
2>      Using this version instead of original version "6.0.0.0" in "F:\packages\Microsoft.Azure.KeyVault.2.0.6\lib\net45\Microsoft.Azure.KeyVault.dll" because AutoUnify is 'true'. (TaskId:19)
2>      Using this version instead of original version "6.0.0.0" in "F:\packages\Microsoft.Azure.KeyVault.WebKey.2.0.4\lib\net45\Microsoft.Azure.KeyVault.WebKey.dll" because AutoUnify is 'true'. (TaskId:19)
2>      Using this version instead of original version "6.0.0.0" in "F:\packages\Microsoft.Rest.ClientRuntime.2.3.2\lib\net45\Microsoft.Rest.ClientRuntime.dll" because AutoUnify is 'true'. (TaskId:19)
2>      Using this version instead of original version "6.0.0.0" in "F:\packages\Microsoft.Rest.ClientRuntime.Azure.3.3.1\lib\net45\Microsoft.Rest.ClientRuntime.Azure.dll" because AutoUnify is 'true'. (TaskId:19)
2>      Resolved file path is "F:\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll". (TaskId:19)
2>      Reference found at search path location "{HintPathFromItem}". (TaskId:19)
2>      Found related file "F:\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.xml". (TaskId:19)
2>      The ImageRuntimeVersion for this reference is "v4.0.30319". (TaskId:19)

Then I've tried to upgrade all these 4 nuget package to the latest version and reinstalled packages in my solution, cleanup solution and rebuild, try again but still failed.

Does anyone ever has the same issue and how to solve this ?

2
Perhaps try to force the package to uninstall. uninstall-package newtonsoft.json -force. The reinstall the package.bolkay
Upgrade your installed .Net framework to at least version 4.6.1 according to the NuGet packages dependencies information: nuget.org/packages/Microsoft.Rest.ClientRuntimeNicoE
@NicoE ,to my understanding the .Net 4.6.1 dependency means minimum 10.0.3 requirement on Newtonsoft.Json , and .Net 4.5.2 requires minimum 6.0.8. So mine .Net does not need upgrade, no ? I have tried to compile targeted to 4.6.1 anyways but the issue still exists.Jeff Wang
@bolkay, just tried but do not work. Thanks for the help though!Jeff Wang
@JeffWang: Yes, I think I'm having the same issue. I have a project with a number of out of date dependencies, including one on Newtonsoft.json 6.0.6. As soon as I update to 12.0.2, it stops working.I don't suppose you ever found a solution to this?dtopham75

2 Answers

0
votes

Just remove any Newtonsoft.Json link from all place:

  • *.csproj
  • packages.config
  • packages folder
  • bin folder
  • any other place which you can find

Close project and then Install package again: Install-Package Newtonsoft.Json -12.0.0

Also, check version Newtonsoft.Json in any other project, if you have link to it.

0
votes

Another option is to let Visual Studio rebuild (correctly) the binding redirects. Here are the steps:

  1. Make a backup (check-in, shelfset, zip, etc)
  2. Open your web.config and remove all of your current binding redirects. Which is pretty-much everything inside of the assemblyBinding section:

<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> *** remove all of the <dependentAssembly> stuff *** </assemblyBinding> </runtime>

  1. Open the Package Manager Console in Visual Studio. Menu: View -> Other Windows -> Package Manager Console menu
  2. Run this command: Get-Project -All | Add-BindingRedirect

This command will rebuild any necessary binding redirects in your project. You can rebuild, run or deploy, and things should be better now.