Header add

In this article we will learn How to solve Could not load file or assembly Newtonsoft.Json error.

Recently we face issue in ASP.NET MVC application while 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. 

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:

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)'



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 expanded the References section in 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

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


Summary

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.

Post a Comment

Previous Post Next Post