Could not load file or assembly Newtonsoft.Json, Version=6.0.0.0 when running unit tests in parallel in VS2017

2

Using VS2017 MVC Web Api project, I have some unit tests which pass fine when run individually, but when run in parallel, I get the below error. I'd like to know how to be able to run these tests in parallel.

System.Reflection.ReflectionTypeLoadException
HResult=0x80131602
Message=Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more 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)":"Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed

The code where the exception occurs is below at s.GetTypes():

using Newtonsoft.Json.Linq;

public static class Factory
{
    private static IEnumerable<MyAbstractClass> GetObjects(JObject parsedJson)
    {
        var list = new List<MyAbstractClass>();
        var type = typeof(MyAbstractClass);
        AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(s => s.GetTypes())
            .Where(p => type.IsAssignableFrom(p) && p.IsClass && !p.IsAbstract)
            .ToList()
            .ForEach(t =>
            {
                var item = (MyAbstractClass)Activator.CreateInstance(t);
                if (item.CanProcess(parsedJson)) list.Add(item);
            });
        return list;
    }
}

I have Newtonsoft.Json version 11.0.0.0 referenced in the project ( so I'm not sure why it's wanting version 6.0.0.0).

I have ensured that the web.config has:

  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
  </dependentAssembly>

During the parallel run, one test (presumably the one that runs first), passes, and all others fail with the identical exception.

c#
unit-testing
json.net
asked on Stack Overflow May 31, 2019 by Jayden

1 Answer

1

I've updated from Newtonsoft.Json version 11.0.1 to 11.0.2 and that appears to have fixed my issue.

answered on Stack Overflow Jun 7, 2019 by Jayden

User contributions licensed under CC BY-SA 3.0