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:
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:
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:
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:
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:
- 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.
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.
Prateek
January 19, 2018Hi, thanks a lot for the solution. I updated the Newtonsoft file as you have outlined and at least managed to fix that one part of the project, after two hours of googling and scratching my hair out!
Igor
July 24, 2018It helped me too, thx! For some reason only one of my projects in the solution was giving that error (which I could only see in the page source), another one had the same version of Newtonsoft but was ok. After I've updated it through nuget both are fine.
Nitesh
August 26, 2018Hi, Thanks for the solution..I was also facing the same problem. After updated the latest version of newtonSoft Json dll from NugetPackage, issue has been resolved.
Steve
April 29, 2019I am having the exact same symptoms, but inside an Excel add-in. It is a single-project solution, so there's nowhere else where I can update a version of the package. Any other idea? Has anyone found out why the References section shows a different version (in my case, 12.0.0.0) than the version of the package (in my case, 12.0.2)?
syam
June 6, 2019it workeddddddddd......thank uuuuuuuuuuuuuuuuu.
Naushad
August 22, 2019Oh thanks now it working.
Leonardo
February 11, 2020Thanks a lot saved so much time!
Vivek
March 9, 2020This did not work for me, I am using 12.0.3 version
Divya
April 1, 2020Thanks a lot, its worked for me and saved much time
Jesus
January 22, 2021Thanks so much man! I wished I have found this post before I had spent a lot of hours searching anywhere else.
James
January 29, 2021Using Update-Package –reinstall Newtonsoft.Json i nthe pakcage manager console worked for me. I was just about to commit some changes when I remembered i needed to undo my Web.Config file (for other dev in the team), I decided to just undo changes on the file, thought I was being smart.
This messed my stuff up haha!
Thanks for this guide! Thought i would comment to say thanks
Dinusha Ranjith
August 11, 2021I have downgraded version up to 12.0.3. And clean and build the solution. problem was resolved
Nick P.
January 7, 2022I wrote a program to fix this: https://github.com/BackTrak/DependencyFixup
Dave
April 12, 2022Worked for me! Thanks
CSBK
January 24, 2023I can not get it working.
This is my first use of json. The application runs on another application: "Could not load file or assembly"
It compiles but fails as soon as it hits main, when I add code: "Dim parseJason As JObject = Nothing"
Michael
February 23, 2023Yess 🙂 This solved the problem.
I have been using days on this problem.
a 1000 thanks
Shahzad
April 11, 2023I am facing this issue while publishing application on plex server.
John L
November 21, 2023YES - a day of searching and finally got your post. Worked like a charm for an SRSS Report I'm doing with Dataverse