'Could not load Newtonsoft.Json. Issue with .net framework / core / standard versions
[EDIT 3: bottom line, this stuff can work but is fragile. take a false step and VS will pull in undesired versions. Be prepared to backup, undo changes, and try again.]
[EDIT 2: I was able to go from an empty solution to a working set of 3 projects. VS2022 was kind and offered to use the same Newtonsoft when I added it to the second project. I give up on fixing the existing projects. Although it will take a ton of work to reconstruct the solution with everything else it had, it's at least a viable path.]
[EDIT 1: It is possible to avoid the issue by not using JSON in the shared DLL, but each client project does this code on their own terms:
someObject s = JsonConvert.DeserializeObject<someObject>(text);
]
This question comes up a lot with tons of different suggested resolutions, none of which are working. Eg. Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. Manifest definition does not match the assembly reference
I think my issue is one of framework resolution. In these other threads, the usual question is of version number. I have a solution with multiple projects targeting different frameworks. One app, "net6api", targets .net6
. It references a .net standard 2.0
project, which in a call invokes HttpClient
to GET a json object. It succeeds.
In a separate NUnit
test DLL targeting .net framework 4.8
, we call the same method in the .net standard project, and here HttpGet yields the above error.
The question is how can I modify the .net framework
DLL project so the build works with the shared standard2.0
DLLs.
Here's the relevant Debugger output:
'testhost.net48.x86.exe' (CLR v4.0.30319: domain-8f64a408-Test.dll): Loaded
'D:\W3\Test\bin\Debug\System.Net.Http.Formatting.dll'.
Exception thrown: 'System.IO.FileLoadException' in System.Net.Http.Formatting.dll
Could not load file or assembly 'Newtonsoft.Json, Version=6.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)
I investigated the build output directories. They appear to have the same System.Net.Http.Formatting.dll
. Newtonsoft.Json.dll
however differs. In the .net6 app build folder it is a 680KB file described to Windows as:
In the .net4.8 test dll folder we have a 686KB file described to windows as:
Exact same version info, different only in File description and size.
So I'm thinking the cause of this is the different frameworks in use at the "top level". I know copying the DLL from one to the other does not fix the other. In other threads there are suggestions to change app.config. That doesn't exist in a test DLL. There is also no package.config as everything using PackageReference. There is no direct reference to NewtonSoft except in the core DLL project.
It's possible there's a thing about dependencies caused by this:
This is the Microsoft.AspNet.WebApi.Client
dependency info The thinking is that the .netframework DLL is looking for a 6.0.x version and failing because this is actually a 13.x version, but that >= should allow it anyway in theory. It's the only reason I could find that 6.0.0 is mentioned in the error message. But I don't know what to do with that info.
EDIT: To work around this, I tried changing the netstd2.0 project to not use Newtonsoft. Instead I used System.Text.Json and System.Net.Http.Json. And the result was similar. .net6 works fine, and .netframework4.8 raises (while in netstd dll code) System.IO.FileLoadException : Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1...
Solution 1:[1]
Hello this is combination of issues, assembly version issue. or, because one of the other packages references Newton.JSON version directly with a wrong version. And the old DLL's are still creating conflict.
Step 1: IMHO, I would global search all your
csproj
files and yourapp.config
files forNewton.JSON
and closely examine the versions for mismatches
Step 2: Ensure you have Binding redirects like so (or just remove the old versions all together)
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<!-- whatever is your current version that should replace the 13.00 below -->
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
</assemblyBinding>
<loadFromRemoteSources enabled="true" />
</runtime>
Step 3: Get rid old stale dll's
- Delete the stale DLL's.
- Then Shudown VS
- Lastly delete the bin and delete you %temp% folder for stale obj folder
Now rebuild a clean version
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Transformer |