VS : How to solve Could not load file or assembly Newtonsoft.Json error

Visual Studio - Json.NET Exception - Could not load assembly

I was working on my ASP.NET MVC application recently and I needed to do some serialization/deserialization work on JSON data. I chose to use Json.NET, a very popular JSON framework for .NET. The problem was that when I ran the application, I was getting an exception telling me that the Newtonsoft.Json could not be loaded due to the assembly's manifest definition not matching the assembly reference. In this article, I will show you what I tried and how I managed to solve the problem.

I installed the Newtonsoft.Json library from a NuGet Package on the class library that was used as a business layer. This way, I was separating the business logic from the ASP.NET MVC project that was on the same solution.

The exact error message, the Visual Studio was giving me was this:

Visual Studio Newtonsoft Json Exception Error Message

System.IO.FileLoadException: 'Could not load file or assembly 'Newtonsoft.Json, Version=10.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)'

On the web, I found a few suggestions.

Checking version in packages.config file

The packages.config file is used by NuGet, a package manager. With this file, the NuGet tracks the packages and their versions installed in the project. When installing the Newtonsoft.Json from the NuGet Manager, it was showing version 10.0.3 and inside the packages.config file, it was also showing the same version:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
</packages>

But, when I expanded the References section of my business layer project in Solution Explorer to check the assembly version, the Properties windows for the Newtonsoft.Json assembly was displaying a different version of 10.0.0:

Newtonsoft Json Reference - version-property in Visual Studio

Click image to enlarge

My first thought was that I located the issue as there seemed to be a mismatch between the version in packages.config and the assembly version. So I edited version in packages.config to also be 10.0.0, but that didn't do anything.

There were also suggestions on the web to check if in the Properties Windows, the "Copy Local" property is set to True, which it was.

So being stuck, I thought, let's try to reinstall the Json.NET package and hope for the best.

Reinstalling the library

First, I reinstalled it using the Nuget Package Manager. When that didn't do the trick, I tried to reinstall the Newtonsoft.Json NuGet package using the Package Manager Console Window.
I first needed to open it at View > Other Windows > Package Manager Console and then typed in the console:

Update-Package –reinstall Newtonsoft.Json

That also didn't solve the problem.

So then I wondered, what would Windows Explorer show, when I check the properties of the Newtonsoft.Json.dll file be in the bin folder of the project and it was showing 10.0.3 version, same one as inside the packages.config file.

I'm not sure why the Properties in Visual Studio was showing the 10.0.0 version, but it seemed the version in the business layer was not the problem. So I turned my attention to the ASP.NET MVC project, since it was referencing the business layer and called objects that use Json.NET.

Newtonsoft.Json in ASP.NET MVC project

I quickly noticed that there was also a reference to Newtonsoft.Json assembly in the ASP.NET MVC project, even though it was not added by me. It turns out, when creating the ASP.NET MVC project, the Visual Studio will automatically add various packages, among them the Newtonsoft.Json library. The problem was that the version Visual Studio installed was the old version 6.0.4, while in the business layer, the version used was 10.0.3.

In the end, the solution was to update it to the latest version, using the following steps:

  1. Open NuGet by right-clicking on on the ASP.NET MVC project and select "Manage NuGet Packages" from the context menu.
  2. After NuGet windows shows up, select the Installed tab.
  3. A list of installed packages will appear. Locate and select the Newtonsoft.Json.
  4. Click on the Update button located on the right side of the window as shown below.

ASP.NET MVC Project NuGet Package Manager - Updating Newtonsoft Json Version

Click image to enlarge

After the library was updated to the same version as the one in other projects, the problem was resolved.

Conclusion

There are many posts on the web regarding the "Could not load file or assembly Newtonsoft.Json" error and how to fix it, some of which were mentioned in this article.

In my case, the problem was caused by Visual Studio, which adds the old 6.0.4 Newtonsoft.Json package when creating the ASP.NET MVC project, while my business layer project was using a newer version of 10.0.3. In the end, the fix was updating the old version of the Newtonsoft.Json package to this new version.

Have you had the same exception while using Json.NET? How did you solve it? Drop a comment and let us know.

18 Comments

Click HERE to add your Comment
  1. Prateek
    January 19, 2018
  2. Igor
    July 24, 2018
  3. Nitesh
    August 26, 2018
  4. Steve
    April 29, 2019
  5. syam
    June 6, 2019
  6. Naushad
    August 22, 2019
  7. Leonardo
    February 11, 2020
  8. Vivek
    March 9, 2020
  9. Divya
    April 1, 2020
  10. Jesus
    January 22, 2021
  11. James
    January 29, 2021
  12. Dinusha Ranjith
    August 11, 2021
  13. Nick P.
    January 7, 2022
  14. Dave
    April 12, 2022
  15. CSBK
    January 24, 2023
  16. Michael
    February 23, 2023
  17. Shahzad
    April 11, 2023
  18. John L
    November 21, 2023

Write a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.