System.BadImageFormatException: Could not load file or assembly or one of its dependencies

2

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.

.net
64-bit
asked on Stack Overflow Dec 19, 2013 by Amitabha • edited May 23, 2017 by Community

2 Answers

10

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

answered on Stack Overflow Dec 19, 2013 by Amitabha
0

In VS2010, it should be

Test - Edit Test Settings - Local - Hosts

also, you need to check Build - Configuration Manager

answered on Stack Overflow Jun 27, 2014 by Eric Wang • edited Jun 27, 2014 by Eric Wang

User contributions licensed under CC BY-SA 3.0