Could not load type 'Newtonsoft.Json.JsonConvert' from assembly Newtonsoft.Json

Viewed 795

I spent a good while on google, so if this is a simple fix forgive me but I cannot find anything relating to a fix to this specific issue within C#.

When trying to use the following code it gives me this error:

string obj = JsonConvert.SerializeObject(playerObject);
Could not load type 'Newtonsoft.Json.JsonConvert' from assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.

The only thing I can really find when searching is people running into version mis-match errors, which is not my issue here as far as I can tell.

Full code for reference: ("logHigh" is a logging function I use)

public void convertToObject(webPlayerObjArray playerObject)
{
   logHigh("Entered function");
   try
   {
      logHigh("Serializing");
      string obj = JsonConvert.SerializeObject(playerObject);
      logHigh(obj);
      webPlayerObjArray plrobjarr = JsonConvert.DeserializeObject<webPlayerObjArray>(obj);
      logHigh(plrobjarr.users[0].badgeName);
   }catch (Exception e)
   {
      logHigh(e.Message);
   }
   logHigh("Finished function");
}
webPlayerObjArray playerFile = new webPlayerObjArray();

webObject webObj1 = new webObject();
webObj1.steamID = "test1";
webObj1.badgeName = "test2";
webObj1.weaponSkinName = "test3";
webObj1.sailName = "test4";
webObj1.cannonSkinName = "test5";

webObject webObj2 = new webObject();
webObj2.steamID = "test1";
webObj2.badgeName = "test2";
webObj2.weaponSkinName = "test3";
webObj2.sailName = "test4";
webObj2.cannonSkinName = "test5";

playerFile.users = new webObject[2];
playerFile.users[0] = webObj1;
playerFile.users[1] = webObj2;
logHigh(playerFile.users[0].badgeName);
logHigh(playerFile.users[1].badgeName);

Class definitions:

[System.Serializable]
public class webObject
{
   public string steamID { get; set; }
   public string badgeName { get; set; }
   public string sailName { get; set; }
   public string weaponSkinName { get; set; }
   public string cannonSkinName { get; set; }
}

[System.Serializable]
public class webPlayerObjArray
{
   public webObject[] users { get; set; }
}

Using visual studio 2019 Addition of Newtonsoft done through NuGet package Manager

  • Version 12.0.3

Object declarations are temporary. Going to switch to loading json from a webpage later on, but i was just trying to get it to work in the first instance, hence the function name.

There's probably an easy fix, but I have unfortunately been unable to find it thus far.

Extra information based on comments and testing:

  • It's a class library for a game mod, not a web project.
  • Adding :
<dependentAssembly>
 <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
 <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.3" /> //12.0.3.23909 also had no effect
</dependentAssembly>

To the app.config unfortunately had no effect, whether present or not.

  • The Newtonsoft.Json reference within VS 2019 has no warning(s)
  • Rebuilding the project has no effect, (had to do this initially as my function refused to run originally as soon as I added any code referencing the DLL, after a successful rebuild I was finally presented with the error above)
  • The dll version that is located in bin\Debug is the same version as referenced within VS 2019
  • Creating a fresh project and copying the code over, did not work
  • Changing target framework (either 4.5 or 4.7.2 (and using respective dll version(s)) had no effect
  • The following previous versions of Newtonsoft.Json were tried, to no success:
    • 12.0.3
    • 11.0.2
    • 10.0.3
    • 9.0.1
    • 8.0.3
2 Answers

I had the same problem, it seams it works only with newversion="12.0.0.0" even if you use Version 12.0.3.23909 of Newtonsoft.Json.

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" /> 
</dependentAssembly>

This what I need to do...

I added Newtonsoft.json to the project and then those directives below the TargetFramework

In dotnet core the <dependentAssembly> it seems is ignored and the those below are the ones that would make it work....

Hope it works for you

The link https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/how-to-enable-and-disable-automatic-binding-redirection about AutoGenerateBindingRedirects

And the GenerateBindingRedirectsOutputType https://github.com/dotnet/msbuild/blob/dd5e8bc3f86ac98bd77d8971b00a6ad14f122f1a/src/XMakeTasks/Microsoft.Common.CurrentVersion.targets#L2027

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Flurl.Http" Version="3.0.1" />
    <PackageReference Include="Newtonsoft.json" Version="13.0.1" />

  </ItemGroup>

</Project>
Related