Test taking dependency on Newtonsoft.Json version other than 9.0.0.0, fail
Create a test project, & add dependency on Newtonsoft.Json version(6.0.0.0)
run the above test using vstest.console.
The test should pass/fail.
Error: System.IO.FileLoadException: System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
@codito how do we address this?
This doesn't occur for LUT scenarios since a new appdomain is not created.
After migrating my .NET Core project to csproj/MSBuild I get this error a lot.
There are also a few other errors that I get which seems very similar:
Message: System.IO.FileLoadException : Could not load file or assembly 'Microsoft.Azure.Documents.Client, Version=1.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Message: System.IO.FileLoadException : Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040
For me this only happens when running the tests in Visual Studio. dotnet test works fine.
I'm using xunit btw.
I created a Visual Studio issue here as well as I'm not sure anymore if this is caused by vstest directly: https://developercommunity.visualstudio.com/content/problem/26347/unit-tests-fail-with-fileloadexception-newtonsoftj-1.html
Try adding these to your property group in the test project's .csproj:
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
@hankbeasley This did it!!! Thanks a lot! I spent hours on this -_-
Is there any explanation why this only fails when running the tests via VS2017?
Related to #428
Issue:: If a user's project is taking a dependency on Newtonsoft.Json with version 6.0.5 and in test code he is using Newtonsoft API's inside an appdomain. Appdomain uses config file of the parent process if not provided any, so here it will use testhost config file which has binding redirect <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />. Since the user's projects is dependent on Newtonsoft.Json of version 6.0.5 their tests break because of the redirection.
In the TestPlatform repo: Microsoft.TestPlatform.CommunicationUtilities depends on Newtonsoft.Json(8.0.3) and Microsoft.TestPlatform.CrossPlatEngine depends on Microsoft.Extensions.Dependencymodel which depends on Newtonsoft.Json (9.0.1). So testhost ends up having Newtonsoft.Json.dll of version 9.0.1 in their publish package. Since testhost uses CommunicationUtilities (and does not use that part of CrossPlatEngine which uses Newtonsoft.Json of version 9.0.1) so it needs Newtonsoft.Json.dll of version 8.0.3 along with it.
Hence, need to copy Newtonsoft.Json.dll of version 8.0.3 to test testhost publish directory. This is however very much dependent on the CrossPlatEngine code running in testhost to not call into APIs specific to 9.0.1.
Most helpful comment
Try adding these to your property group in the test project's .csproj:
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>