Failed to load file or assembly "Newtonsoft.Json, Version = 6.0.0.0 in unit test

I am trying to unit test a project that has dependencies on Json.Net and SignalR. For some reason, I cannot run many of these unit tests. Since I upgraded Json.Net to version 9.0.0, I get an exception that says:

An exception like "System.IO.FileLoadException" occurred in mscorlib.dll but was not handled in the user code

Additional information: Failed to load the file or assembly "Newtonsoft.Json, Version = 6.0.0.0, Culture = neutral, PublicKeyToken = 30ad4fe6b2a6aeed" or one of its dependencies. The located assembly manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)


I can reproduce this behavior with a clean project. Here are the steps to play:

  • Use Visual Studio 2015 Update 3
  • Go to File-> New-> Project
  • Choose Templates-> Visual C # β†’ Test β†’ unit test Project
  • Right-click the project, select properties and change the structure to .NET Framework 4.6.1
  • create a new file in the root of the project named project.json
  • Set the contents of project.json as follows:

.

{ "dependencies": { "Microsoft.AspNet.SignalR.Client": "2.2.1", "Microsoft.AspNet.SignalR.Core": "2.2.1", "Newtonsoft.Json": "9.0.1" }, "frameworks": { "net451": {}, "net461": {} }, "runtimes": { "win": {}, "win-x86": {}, "win-x64": {} } } 
  • Change UnitTest1.cs (provided via scaffolding) to the following:

.

 using Microsoft.VisualStudio.TestTools.UnitTesting; namespace UnitTestProject1 { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { var conman = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager; } } } 
  • Close and reopen visual studio (this is necessary to load dependencies using project.json)
  • Restore project
  • select a testing method and press CTRL + R + T

An error should appear.

I have no idea how to fix this. I tried playing with bindingRedirects and nothing made the error go away. Reinstalling the package makes no sense, because I can reproduce this with a clean project.

My fear is that I will have to revert to an earlier version of Json.Net

+6
source share
3 answers

So, I asked to start a test project. I'm not quite sure what specifically made it work, since I tried a few things, but I guess this is a combination of these two things:

flushing the nuget cache as described here: How to flush the NuGet package cache using the command line?

First download the nuget command line tool from here .

Then open the command prompt and cd to the directory where nuget.exe was downloaded.
...
You can clear all caches with this command:
nuget locals all -clear

copying the binding attributes from the project where I used the package.config paradigm instead of project.json.

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> 

Update: I had other projects, including UWP projects, which also had problems loading Newtonsoft.Json. In the end, I just went back to 7.0 and all my problems went. The moral of the story: trying to resolve addictions is pain.

Update: After some similar experiments in other projects, it seems that the choice of cleaning from VS does not always completely clear the bin \ debug folder, and manually deletes this folder.

+4
source

Sometimes Newtonsoft.Json (useless) terminates in the global assembly cache (GAC) and it will surpass any version you ask for if it is not listed locally in this directory. Check the links of your test project and find the link to Newtonsoft.Json:

  • Verify that Reference Local is set to True .
  • Make sure the link points to the correct NuGet DLL in the folder with your solutions / packages, and not somewhere else.
+1
source

There seems to be a discrepancy between the assemblies in your projects (unit test project and target project).

I ran into a similar problem before, and the solution was to combine the versions into one library. You can do this by right-clicking your solution and then β€œManage NuGet Packages for Solution”. This will allow you to choose which version you want to use for projects under your decision, which depend on this or any other library.

0
source

Source: https://habr.com/ru/post/1011890/


All Articles