7

I am trying to unit test a project that has dependencies on Json.Net and SignalR. For some reason I am unable to get many of these unit tests to run. Ever since I've updated Json.Net to version 9.0.0, I am getting an exception that says:

An exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll but was not handled in user code

Additional information: 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 can reproduce this behavior with a clean project. Here are the steps to reproduce:

  • Use Visual Studio 2015 Update 3
  • Go to File->New->Project
  • Select Templates->Visual C#-> Test -> Unit Test Project
  • Right click the project, select properties, and change the framework to .NET Framework 4.6.1
  • create a new file at the root of the project named project.json
  • Set the content of project.json to the following:

.

{
  "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 by the 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 the dependencies using project.json)
  • Rebuild the project
  • select the test method, and press CTRL+R+T

The error should appear.

I have no idea how to fix this. I've tried playing around with the binding bindingRedirects, and nothing has made the error go away. Reinstalling the package doesn't make any sense, because I can reproduce this with a clean project.

My fear is that I'll have to revert back to an earlier version of Json.Net

  • 1
    That's a *very old* version of Json.NET. Either add the old version to your test project or upgrade *all your projects* to use the same version. Did you check the "Update" or "Consolidate" tabs in the NuGet Package Manager ? – Panagiotis Kanavos Oct 31 '16 at 16:29
  • Also check for direct references to Json.NET, ie not through NuGet – Panagiotis Kanavos Oct 31 '16 at 16:31
  • @PanagiotisKanavos I am reproducing this with a clean project in a brand new solution that has absolutely nothing but that unit test. – Sam I am says Reinstate Monica Oct 31 '16 at 16:32
  • The ASP.NET templates don't contain the latest Json.NET references. The error occurs because the *web app* references the old Json.NET version. Remove any existing non-Nuget references and add the package to all projects. I *have* encountered the same issues in the past – Panagiotis Kanavos Oct 31 '16 at 16:36
  • it's also possible that the template uses a GAC reference for JSON.NET. That's why you should check for existing references, then make sure you use NuGet only – Panagiotis Kanavos Oct 31 '16 at 16:38
  • You have a project.json file - I thought that was only used in the pre-release versions of Visual Studio and they switched back to .csproj projects at RTM. – Joe Enos Oct 31 '16 at 16:57
  • SignalR 2.2.1 still references 6.0.4 JSON.net - https://www.nuget.org/packages/Microsoft.AspNet.SignalR.Core/2.2.1 - looks like you may need binding redirects to get it working. – Pawel Oct 31 '16 at 17:43
  • Add assembly redirect into the test project app.config file to point to v9. – Alexey Zimarev Oct 31 '16 at 20:24

3 Answers3

4

So, I got the test project to run. I'm not entirely sure what specifically got it to work since i tried multiple things, but I'm guessing it was a combination of these two things:

clearing the nuget cache as described here: How to clear NuGet package cache using command line?

First, download the nuget command line tool from here.

Next, open a command prompt and cd to the directory to which nuget.exe was downloaded.
...
You can clear all caches with this command:
nuget locals all -clear

copying the bindingredirects from a project where I used the packages.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, that were also having issues loading Newtonsoft.Json. In the end, I just reverted back to 7.0, and all my issues went a way. Moral of the story: Trying to resolve dependencies is pain.

Update: After some similar experiences in other projects It seems as though selecting clean from VS isn't always fully cleaning the bin\debug folder, and manually deleting that folder seems to help.

Community
  • 1
  • 1
1

Sometimes Newtonsoft.Json (unhelpfully) winds up in the Global Assembly Cache (GAC) and it will trump whatever version you're asking for unless it's provided locally in that directory. Check the references of your test project and find the reference to Newtonsoft.Json:

  • Make sure the reference's Copy Local value is set to True.
  • Make sure the reference is pointed at the correct NuGet DLL in your solution's /packages folder rather than somewhere else.
UtopiaLtd
  • 2,520
  • 1
  • 30
  • 46
  • The reference does not have a "copy local" value. In fact it does not have any properties at all. It appears as though that has something to do with using project.json to define it as a dependency. – Sam I am says Reinstate Monica Oct 31 '16 at 16:42
  • So the reference has a NuGet icon then, right? Is there a Newtonsoft.Json.dll in the unit test directory itself? If so, what version does it report if you open its properties in the Windows file explorer? If you somehow aren't really on Update 3, you could try adding `true` to a new PropertyGroup in your .csproj file, but that supposedly has been fixed with Update 1. – UtopiaLtd Oct 31 '16 at 17:47
  • Newtonsoft.dll in the bin/debug directory has a file version of `9.0.1.19813` – Sam I am says Reinstate Monica Oct 31 '16 at 17:55
  • And in case you have any further questions, I have provided the complete steps to reproduce this issue with a clean project, so that you can experience the issue yourself. – Sam I am says Reinstate Monica Oct 31 '16 at 17:56
  • Right, I followed those and had no incident. At this point, it looks like your test runner's working directory either isn't really the bin folder and it's still pulling from the GAC or binding redirects aren't working. Easy thing to do is just remove it from your GAC, but if this is going out to clients, obviously that won't be appropriate. – UtopiaLtd Oct 31 '16 at 18:10
0

It seems that there is an divergence between assemblys in your projects (unit test project and target project).

I've faced an similar issue before and the solution was to consolidate the versions into one library. You can do this by right clicking your solution and then "Manage NuGet Packages for Solution". This will let you choose what version you want to use for the projects under your solution that dependes on this or any other library.