5

I'm attempting to parse a JSON file in Class Library within an Web API solution. It is a regular C# Class Library, not the Portable kind.

I've tried every single answer mentioned here, but it still doesn't work! I keep getting the same error which is:

Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed

Here is the code:

public IList<BranchRM> AllBranches()
{
    var result = new List<BranchRM>();
    var dataSourcePath = AppDomain.CurrentDomain.BaseDirectory + "Data/branches.json";
    var dataAsText = File.ReadAllText(dataSourcePath);
    if (string.IsNullOrEmpty(dataAsText)) return result;
    var branchList = JsonConvert.DeserializeObject<List<Branch>>(dataAsText);
    result = AutoMapper.Mapper.Map<List<BranchRM>>(branchList);
    return result;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
J86
  • 14,345
  • 47
  • 130
  • 228
  • have you tried Reinstalling `Newtonsoft.Json` via `Nuget` ? – tchelidze Jan 13 '16 at 14:27
  • Yep by doing `Update-Package Newtonsoft.Json -Reinstall`, still didn't work. – J86 Jan 13 '16 at 14:29
  • You can add reference to `Newtonsoft.Json.dll` directly, without `Nuget`. it resides in same directory as `Solution` under `Packages` Folder. – tchelidze Jan 13 '16 at 14:34
  • Thanks @tchelidze I just did, but still get the same error :( – J86 Jan 13 '16 at 14:38
  • Have updated `Newtonsoft.Json` package recently ? Probably, it is cached in `GAC` and `VS` tries to load from `GAC`, but since there is version mismatch(`GAC` stores older version than you have referenced currently), you get following error. Try to find `Newtonsoft.Json` in `GAC` and delete it. – tchelidze Jan 13 '16 at 14:40
  • Try to check GAC for Newtonsoft.Json, Version=8.0.0.0 – Michael Jan 13 '16 at 14:42
  • I just looked at the GAC folders (through Agent Ransack), but found no files with the word `Newtonsoft` :( – J86 Jan 13 '16 at 14:47
  • Hey @Ciwan ...Did you ever got this resolved ? – Amit Tyagi Feb 09 '16 at 20:28
  • @AmitTyagi sadly I did not :( – J86 Feb 10 '16 at 11:26

2 Answers2

5

I was fixing some old code in one of my Windows Phone 8 solutions and thought of updating the NuGet packages and was greeted with the same issue.

A comment from StivOstenberg here helped me solve this.

This is what I did:

  1. Removed the NuGet package. Just clicked 'Uninstall' from NuGet manager. Make sure to remove it from every project in the solution separately.
  2. Clean Solution. Rebuild Solution.
  3. Now, remove the using statement from your entire solution! Can be done with a quick find and replace for using Newtonsoft.Json on 'Entire Solution'.
  4. Repeat step 2. (Ignore the errors)
  5. Add package again from NuGet manager & build (Ctrl+Shift+B).
  6. Finally (almost), for every error shown, go to the particular page and add reference again.
  7. Repeat step 2 and Run.

There might be some redundant steps, but this is exactly what I did, and it worked. Hope it helps you too.

sid
  • 164
  • 5
  • 14
1

Make sure there are no conflicting versions of Newtonsoft in a parent assembly!

In a child-assembly I wanted to use Newtonsoft.Json.8.0.3.

Well the StartUp project is an MVC5 web app. There I use the BundleTransformer.Less.X.X.X which had a dependency to Newtonsoft.Json.8.0.2. Upgrading NewtonSoft.Json to 8.0.3 (all the same version now) resolved it for me.

Andy R
  • 1,339
  • 10
  • 20