2
votes

I have a similar error to System.BadImageFormatException: Could not load file or assembly. But the condition to fire the error is different.

Test Name: Test_DisableTestUserAccount
Test FullName:
Test Source:
Test Outcome: Failed
Test Duration: 0:00:00

Result Message:
Unable to get type #typename#. Error: System.IO.FileLoadException: Could not load file or assembly 'ComponentName.IntTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a433f9a8cee952' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)
File name: 'ComponentName.IntTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=433f9a8cee952' ---> System.Security.SecurityException: Strong name validation failed. (Exception from HRESULT: 0x8013141A)
The Zone of the assembly that failed was:
MyComputer
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at Microsoft.VisualStudio.TestPlatform.MSTestFramework.TypeCache.LoadType(String typeName, String assemblyName)

Conditions:

  1. The solution has the C# console application and its UnitTest project
  2. The InternalsVisibleTo is enabled from console application to Unit project
  3. All project are stronge name signed
  4. The sln platform has two configuration : x64 and x86 (the others are removed)

When I run the Unit Test case who will access the internal elements of the console app, the test case will fail and throw above exception.

2
Explicitly targeting x64 in your project is never useful. Always pick AnyCPU, only pick x86 for your EXE project if you have a dependency on unmanaged code.Hans Passant
Our system is compiled to x64, that's our release target. Thank you anyway.Amitabha

2 Answers

10
votes

The root cause to the problem is that I use the x64 platform to build the sln and to run test case with x86 test setting.

Just use the correct test setting platform to run test case:

  • Build platform should be same as the platform to run the test case.

Screenshot: enter image description here

0
votes

In VS2010, it should be

Test - Edit Test Settings - Local - Hosts

also, you need to check Build - Configuration Manager