0

[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:

Json Standard dll

In the .net4.8 test dll folder we have a 686KB file described to windows as:

Json.NET dll

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: enter image description here

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...

Alan Baljeu
  • 2,383
  • 4
  • 25
  • 40
  • Your Framework 4.8 test harness (the `testhost.net48.x86.exe` from your screenshot above) will have a `.config` file in the build output folder - look in that file for the appropriate assembly redirect for Newtonsoft.Json. If you don't find the assembly redirect, add the Newtonsoft.Json package to your test harness and rebuild. Also make sure "Auto-Generate Binding Redirects" is selected in the application properties. – Mr. T Apr 29 '22 at 18:20
  • The build output has app.config. It does not have any redirects. The test harness is NUnit. I use it, I don't modify it, so this instruction to add to the test harness doesn't seem like a thing I know how to do. – Alan Baljeu Apr 30 '22 at 14:28
  • Or maybe NUnit is just the library? I run the test from DevStudio Test explorer, but still, I am not in control of the App that runs my test DLL. – Alan Baljeu Apr 30 '22 at 14:30
  • 1
    you are probably missing the binding redirect `` in the app.config file – Akshay G May 04 '22 at 12:56
  • I don't have any app.config files. Where do I put this? – Alan Baljeu May 04 '22 at 13:20
  • @AlanBaljeu Try adding the following to the *.config of the calling project ` ` – Kishan Vaishnani May 07 '22 at 09:50

1 Answers1

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 your app.config files for Newton.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

  1. Delete the stale DLL's.
  2. Then Shudown VS
  3. Lastly delete the bin and delete you %temp% folder for stale obj folder

Now rebuild a clean version

Transformer
  • 6,963
  • 2
  • 26
  • 52
  • Instructions followed. All project associated files now reference only 13.0.1. I put the binding redirect into my test project config. All newtonsoft.json.dll deleted from under solution folder. %temp% contents deleted. VS restarted. Build. Still get MSB3277, conflicts cannot be resolved. – Alan Baljeu May 09 '22 at 13:30
  • 6> References which depend on "Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" [C:\Users\Owner\.nuget\packages\newtonsoft.json\6.0.4\lib\net45\Newtonsoft.Json.dll]. 6> C:\Users\Owner\.nuget\packages\newtonsoft.json\6.0.4\lib\net45\Newtonsoft.Json.dll 6> Project file item includes which caused reference "C:\Users\Owner\.nuget\packages\newtonsoft.json\6.0.4\lib\net45\Newtonsoft.Json.dll". 6> C:\Users\Owner\.nuget\packages\newtonsoft.json\6.0.4\lib\net45\Newtonsoft.Json.dll – Alan Baljeu May 09 '22 at 13:48
  • @AlanBaljeu 1) did you delete _Your project_ build dlls, not just newton? 2) try to remove [all the binding redirects completely](https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/how-to-enable-and-disable-automatic-binding-redirection), delete all the dll's and then _Enter the command `Add-BindingRedirect` in your console (you can also specify a target project using -ProjectName "SpecificProject");_ – Transformer May 10 '22 at 21:08
  • Yes I deleted all build outputs, bin and obj folders. I removed all binding redirects. I've never head of the Add-BindingRedirect command – Alan Baljeu May 11 '22 at 13:25