Search code examples
c#.netjsonnuget-package-restoreappveyor

Appveyor, test reference the wrong assembly version


New to the Appveyor platform, so this might just be my naivety. I set up my project to build from my GitHub repo. This seems to be working, except for the tests. The code is mainly .Net 4.0, as it is supporting a legacy project - I can't move it to a later framework at the moment. Because this project is using Newtonsoft.Json already, I had to use a specific version. All code and test run locally. However, once I set up the CI (and got my head around getting the Nuget restore to happen), I still get a complete failure with the tests. They aren't doing anything spectacular. The error is pretty straight forward - the Nuget is pulling in version 6.0.8, but the build process for the tests wants to reference 8.0.0.0 according to the output:

Discovering tests...OK vstest.console /logger:Appveyor "C:\projects\debuginterface\Ratcow.Debugging.Server.Tests\bin\Debug\Ratcow.Debugging.Server.Tests.dll" Microsoft (R) Test Execution Command Line Tool Version 15.0.26228.0 Copyright (c) Microsoft Corporation. All rights reserved. Starting test execution, please wait... Failed BasicTest_ValueTypes Error Message: Test method Ratcow.Debugging.Server.Tests.MainUnitTest.BasicTest_ValueTypes threw exception: System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=8.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) ---> System.IO.FileLoadException: 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) Stack Trace: at Ratcow.Debugging.Server.DebugInterface.InstanceAsString(Object value) at Ratcow.Debugging.Server.DebugInterface.GetVariableValue(String variableName) in C:\projects\debuginterface\Ratcow.Debugging.Server\DebugInterface.cs:line 104 at Ratcow.Debugging.Server.Tests.MainUnitTest.BasicTest_ValueTypes() in C:\projects\debuginterface\Ratcow.Debugging.Server.Tests\MainUnitTest.cs:line 35

I can accept that this is probably correct - except the code is referencing 6.0.8, and so is the packages.config :

<?xml version="1.0" encoding="utf-8"?>
 <packages>
     <package id="MSTest.TestAdapter" version="1.1.11" targetFramework="net452" />
     <package id="MSTest.TestFramework" version="1.1.11" targetFramework="net452" />
     <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net40" />
</packages>

Any ideas? The last failed build with tests enabled is here. It is happily building and tests are running on VS2017 on multiple desktop and laptops.


Solution

  • Okay, thanks to Jeroen's prompt above (I was going to try this, but I didn't get time earlier before posting this question), the answer is two fold:

    1. This is not Appveyor specific. My tests also failed for the exact same reasons if I ran vstest.console.exe directly upon the compiled test project. This was a big part in me fixing the issue, recreating issues is always the biggest part of fixing them.
    2. As suggested, adding in a dependentAssembly declaration really worked. Here's what I added to an App.config for the test project:

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
          <dependentAssembly>
              <assemblyIdentity name="Newtonsoft.Json"
              <publicKeyToken="30ad4fe6b2a6aeed" />
              <bindingRedirect oldVersion="8.0.0.0" newVersion="6.0.0.0" />
          </dependentAssembly>
      </configuration>
      

    The tests now run on the command line, and having just committed the code to GitHub, it looks like the Appveyor also builds and runs the test too.