0

Our C# application uses Nuget package Microsoft.Rest.ClientRuntime. As one of dependencies this Nuget package has installed Newtonsoft.Json Nuget package. Currently it looks like the following:

enter image description here

As I see the version of the Newtonsoft.Json assembly is 12.0.3.

After running the application I get the exception error:

Could not load file or assembly Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b

I really cant understand why it requires v. 10 if that should be v. 12?

The assembly that was copied to the output folder is v.12:

enter image description here

After long searching in the Internet I've tried to open the dll in the Net Reflector and so find that Microsoft.Rest.ClientRuntime assembly looks for Newtonsoft.Json v. 10 and not for v. 12 as that should be:

enter image description here

I've tried to update all the Nuget packages for the solution but that didn't help.

So my question - how can I fix that? Or how can I point it to the right assembly version?

Environment:

  • Visual studio 2019
  • .Net 4.7.2
  • Windows 10 x64
folibis
  • 12,048
  • 6
  • 54
  • 97
  • What is the whole error about this, please share the whole with us? – Mr Qian Oct 13 '20 at 08:10
  • If I open the latest Microsoft.Rest.ClientRuntime in nuget package explorer it has a requirement like this: Newtonsoft.Json >= 10.0.3, and your screenshot shows 12.0.3 so most likely you have another nuget package that requires a newer version of Newtonsoft.Json, I would check that first. If you have packages that each requires a different version of Newtonsoft.Json then you must use binding redirects in web.config/app.config and redirect to the newest version. Would never recommend trying to GAC the assemblies as recommended in one of the answers. – Thomas Schmidt Oct 13 '20 at 09:34
  • Thanks @ThomasSchmidt, it looks that you're right. I've uninstalled both `Microsoft.Rest.ClientRuntime` and `Newtonsoft.Json` from all the projects and then installed `Microsoft.Rest.ClientRuntime` first. So the `Newtonsoft.Json` that was installed as a dependency was v. 10.0.0.0. For all other projects that use `Newtonsoft.Json` I've downgraded to v.10 too and that solved the issue. Workaround but works. Thank you guys for your help! – folibis Oct 13 '20 at 09:49
  • Thanks for Thomas's guidance and I am grateful for what he did. So I have added the point into my answer. – Mr Qian Oct 14 '20 at 02:09
  • @folibis, I have updated my answer with the important tips and workaround. You can check it. If it helps, please do not forget to accept it so that it will help other community members handle similar issues:) – Mr Qian Oct 14 '20 at 02:10

1 Answers1

1

Actually, when you reference Newtonsoft.Json v12.0.3, the right version of the dll is 12.0.0.0.

enter image description here

To troubleshoot the issue,please try these:

1) clean nuget caches first or delete all caches files under C:\Users\xxx(current user)\.nuget\packages

2) close VS delete .vs hidden folder under the solution folder, bin and obj folder.

3) enter the nuget package Newtonsoft.Json v12.0.3, usually under <solution folder>\packages\Newtonsoft.Json.12.0.3\lib\net45.

Run Developer Command Prompt for VS2019 as Administrator and then enter the path under it.

type these to register Newtonsoft.Json.dll into GAC:

cd xxxxx (the path of the the Newtonsoft.Json 12.0.0.0)

gacutil /i Newtonsoft.Json.dll

4) or you could add bindredirect in config file:

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

Then, rebuild your project to test whether the issue still persists.

More info, you can refer to this similar issue.

===================================

Update 1

Solution

First, uninstall Microsoft.Rest.ClientRuntime and Newtonsoft.Json nuget package on your project to make a clean environment.

Then, first install Microsoft.Rest.ClientRuntime nuget package first which will also install the dependency Newtonsoft.Json 10.0.3 at the same time.

enter image description here

Make sure that you have downgrade Newtonsoft.Json nuget package to 10.0.3 version so that it will solve the issue.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41