Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install version of json.net compatible with .net 4.0?

To avoid the issue described here:

Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib

I'm trying to build my project to target .net 4.0 instead of 4.5 (in Visual Studio 2013), but I get the following errors:

  • The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)

    The primary reference "Newtonsoft.Json" could not be resolved because it was built against the ".NETFramework,Version=v4.5" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.0".

So I would like to install a version of json.net that is compatible with .net 4.0. I see that the downloaded NuGet package contains a lib/4.0 directory in addition to lib/4.5, but I can't figure out how to get the package manager to use this instead of the 4.5 version.

How can I get my project solution to buikd using the version of json.net compatible with .net 4.0 instead of the one that only works with .net 4.5?

like image 244
Jake Avatar asked Nov 25 '13 17:11

Jake


2 Answers

Run update-package -reinstall from the package manager console.

like image 130
Reda Avatar answered Oct 28 '22 08:10

Reda


The way I found to do this is:

  1. Close your VS solution
  2. Manually edit all the packages.config files in your solution to point to the version that you want (e.g. for me I changed them to id="Newtonsoft.Json" version="6.0.3" targetFramework="net40" )
  3. Go to the packages folder on disk for your solution and delete the version in there
  4. Delete the Newtonsoft.Json dll and xml from the output path for your solution
  5. Delete the bin directories for the projects that use json.net in your solution
  6. open the solution in visual studio
  7. open the package manager (use menus: tools, NuGet package manager, manage NuGet packages for solution in my version of VS)
  8. At the top of package manager dialog, it complains about mssing packages - click on Restore
  9. Go to the packages folder on disk for your solution and check that it installed the correct version
  10. Clean the solution (otherwise the Newtonsoft.Json dll and xml will not be replaced in the output path)
  11. Build and check that the DLL in the output path is the correct version and for the correct framework (NewtonSoft puts the .net version in the file description for the older versions)

If any of these steps could be optimised I'd love to know how...

like image 37
danio Avatar answered Oct 28 '22 07:10

danio