Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newtonsoft.Json version 8.0.2 Could not load file or assembly Error

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;
}
like image 971
J86 Avatar asked Jan 13 '16 14:01

J86


1 Answers

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.

like image 64
sid Avatar answered Oct 25 '22 02:10

sid