8

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?

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

That will install the newest version of Newtonsoft.Json package (which includes all targets: .net2.0, 3.5 4.0, 4.5, portable4.0 and portable 4.5) then select the lib targetting you current csproj settings.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.