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 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:
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 few suggestions.
Checking version in packages.config file
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"?>
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
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:
Click 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 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
I'm not sure why the Properties in Visual Studio was showing 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:
- 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.
Click to Enlarge
After the library was updated to the same version as the one in other project, the problem was resolved.
There are many posts on the web regarding "Could not load file or assembly Newtonsoft.Json" error and how to fix it, some of which was mentioned in this article. in my case, the problem was caused by Visual Studio, which adds 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 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.