How to Fix Json.Net (Newtonsoft.Json) Runtime File Load Exception

How to fix json.net (Newtonsoft.Json) runtime file load exception

Thanks everyone, I found the problem. I did not add culture="neutral" when I put this in web.config.

<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" PublicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
</dependentAssembly>

Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. Manifest definition does not match the assembly reference

Ok, I think I got it to work now. I deleted every Newtonsoft.Json.dll on my machine that wasn't the latest version that I could find, made sure I had the latest version in NuGet, and build it and made sure that was the latest one in the bin folder, and I left the changes in the web.config and the .csproj. Now I'm on to another error, so it must be working..

Could not load file or assembly Newtonsoft.Json when running app from the dotnet publish output folder

I solved the mystery. It had nothing to do with Docker. It kind of had something to do with dotnet publish but the SDK works well.

The problem was, as initially suspected, with a version conflict. When publishing with dotnet publish -c Release -o publish I could see the Newtonsoft.Json.dll there. But the following made me suspicious

ls publish/ -al | grep Newtonsoft
-rwxrw-r-- 1 diegosasw 89K mar 22 2017 Newtonsoft.Json.Bson.dll*
-rwxrw-r-- 1 diegosasw 641K mar 24 2018 Newtonsoft.Json.dll*

2018 seems a bit old for that version. What if.. that Newtonsoft.Json assembly being published wasn't the version 11.0.2 after all?

I updated all my libraries to match MassTransit dependency on Newtonsoft 11.0.2 but my findings on Update 4 made me think there was some other project depending indirectly on Newtonsoft.Json 9.0.1, and that's why I could see that nuget package being cached locally.

If, somehow, the assembly being published is not the expected Newtonsoft.Json 11.0.2 but the Newtonsoft.Json 9.0.1, the error would make sense when complaining about not finding the assembly Newtonsoft.Json 11.0.2

Bingo!

I installed exiftool in my Ubuntu to check dll and exe versions.

sudo apt install libimage-exiftool-perl

I run the following

$ exiftool publish/Newtonsoft.Json.dll
ExifTool Version Number : 11.88
File Name : Newtonsoft.Json.dll
Directory : publish
File Size : 465 kB
File Modification Date/Time : 2021:07:19 19:52:18+02:00
File Access Date/Time : 2021:10:04 12:53:14+02:00
File Inode Change Date/Time : 2021:10:04 12:44:39+02:00
File Permissions : rwxrw-r--
File Type : Win32 DLL
File Type Extension : dll
MIME Type : application/octet-stream
Machine Type : Intel 386 or later, and compatibles
Time Stamp : 2016:06:13 13:05:00+02:00
Image File Characteristics : Executable, Large address aware, DLL
PE Type : PE32
Linker Version : 48.0
Code Size : 465920
Initialized Data Size : 2048
Uninitialized Data Size : 0
Entry Point : 0x738b6
OS Version : 4.0
Image Version : 0.0
Subsystem Version : 4.0
Subsystem : Windows command line
File Version Number : 9.0.1.19813
Product Version Number : 9.0.1.0
File Flags Mask : 0x003f
File Flags : (none)
File OS : Win32
Object File Type : Dynamic link library
File Subtype : 0
Language Code : Neutral
Character Set : Unicode
Comments : Json.NET is a popular high-performance JSON framework for .NET
Company Name : Newtonsoft
File Description : Json.NET .NET Standard 1.0
File Version : 9.0.1.19813
Internal Name : Newtonsoft.Json.dll
Legal Copyright : Copyright © James Newton-King 2008
Legal Trademarks :
Original File Name : Newtonsoft.Json.dll
Product Name : Json.NET
Product Version : 9.0.1
Assembly Version : 9.0.0.0

and as you can see, the published assembly is Newtonsoft.Json 9.0.1. A mix of relief invades me.

I went to my test projects and added the following to the *.csproj

<IsPublishable>false</IsPublishable>

and

rm -rd publish
dotnet publish -c Release -o publish

Moment of truth

$ exiftool publish/Newtonsoft.Json.dll
ExifTool Version Number : 11.88
File Name : Newtonsoft.Json.dll
Directory : publish
File Size : 641 kB
File Modification Date/Time : 2018:03:24 18:44:14+01:00
File Access Date/Time : 2021:10:04 12:44:38+02:00
File Inode Change Date/Time : 2021:10:04 12:57:29+02:00
File Permissions : rwxrw-r--
File Type : Win32 DLL
File Type Extension : dll
MIME Type : application/octet-stream
Machine Type : Intel 386 or later, and compatibles
Time Stamp : 2098:12:14 20:33:48+01:00
Image File Characteristics : Executable, Large address aware, DLL
PE Type : PE32
Linker Version : 48.0
Code Size : 653824
Initialized Data Size : 2048
Uninitialized Data Size : 0
Entry Point : 0xa16b6
OS Version : 4.0
Image Version : 0.0
Subsystem Version : 4.0
Subsystem : Windows command line
File Version Number : 11.0.2.21924
Product Version Number : 11.0.2.0
File Flags Mask : 0x003f
File Flags : (none)
File OS : Win32
Object File Type : Dynamic link library
File Subtype : 0
Language Code : Neutral
Character Set : Unicode
Comments : Json.NET is a popular high-performance JSON framework for .NET
Company Name : Newtonsoft
File Description : Json.NET .NET Standard 2.0
File Version : 11.0.2.21924
Internal Name : Newtonsoft.Json.dll
Legal Copyright : Copyright © James Newton-King 2008
Legal Trademarks :
Original File Name : Newtonsoft.Json.dll
Product Name : Json.NET
Product Version : 11.0.2
Assembly Version : 11.0.0.0

Now the assembly published is the expected 11.0.2.

I verify that running my app from the publish folder now works fine!

cd publish
dotnet MyCompany.ItgService.dll

How to fix issue with Newtonsoft.JSON dependency?

Please verify if the version of System.Runtime.Serialization currently is 4.1.1.

Or try to install via nuget package console by using this command:

Install-Package System.Runtime.Serialization.Primitives -Version 4.1.0

Newtonsoft.Json issue: could not load file or assembly exception from source System.Net.Http.Formatting

Writing here, because better formatting ... i just want to add to @Richard's comment.

In your app.config, you will have some lines as shown below (may look a little different):

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

Change that version number to the version you need i.e. '11.0.1' (or '12.0.3', current stable release).

Why could this JSON.Net FileLoadException be conditional

I got the dll versions to match all across and it works now.

I thought I had the same version between my class library project and console app project because I had uninstalled and reinstalled using the package manager (which was ok) then selected the latest version of the dll for the console project using the package manager, just searching Assemblies for json and selecting my framework. That was my downfall. I don't know why 6.0 was the latest offered, see below:
Reference Manager screenshot showing latest dll to add
If I just browsed my project/package directory I could just add the 9.0.1 that I was using there, then the serialization in the console project worked.
Thanks for the link @dbc



Related Topics



Leave a reply



Submit