Fix for ‘System.BadImageFormatException’ when running x64 based tests inside a Azure DevOps Release

This is one of those blog posts I write to remind my future self how I fixed a problem.

The Problem

I have a release that installs VSTest and runs some integration tests that target .NET 4.6 x64. All these tests worked fine in Visual Studio. However, I got the following errors for all tests when they were run in a release

 12020-04-23T09:30:38.7544708Z vstest.console.exe "C:agent\_workr1aPaymentServicesdroptestartifactsPaymentService.IntegrationTests.dll"
 2
 32020-04-23T09:30:38.7545688Z /Settings:"C:agent\_work\_tempuxykzf03ik2.tmp.runsettings"
 4
 52020-04-23T09:30:38.7545808Z /Logger:"trx"
 6
 72020-04-23T09:30:38.7545937Z /TestAdapterPath:"C:agent\_workr1aPaymentServicesdroptestartifacts"
 8
 92020-04-23T09:30:39.2634578Z Starting test execution, please wait...
10
112020-04-23T09:30:39.4783658Z A total of 1 test files matched the specified pattern.
12
132020-04-23T09:30:40.8660112Z   X Can\_Get\_MIDs \[521ms\]
14
152020-04-23T09:30:40.8684249Z   Error Message:
16
172020-04-23T09:30:40.8684441Z    Test method PaymentServices.IntegrationTests.ControllerMIDTests.Can\_Get\_MIDs threw exception: 
18
192020-04-23T09:30:40.8684574Z System.BadImageFormatException: Could not load file or assembly 'PaymentServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
20
212020-04-23T09:30:40.8684766Z   Stack Trace:
22
232020-04-23T09:30:40.8684881Z       at PaymentServices.IntegrationTests.ControllerMIDTests.Can\_Get\_MIDs()
24
25
26
272020-04-23T09:30:40.9038788Z Results File: C:agent\_work\_tempTestResultssvc-devops\_SVRHQAPP027\_2020-04-23\_10\_30\_40.trx
28
292020-04-23T09:30:40.9080344Z Total tests: 22
30
312020-04-23T09:30:40.9082348Z      Failed: 22
32
332020-04-23T09:30:40.9134858Z ##\[error\]Test Run Failed.

Solution

I needed to tell vstest.console.exe to run x64 as opposed to it’s default of x32. This can be done with a command line override –platform:x64

image