System.BadImageFormatException:无法加载文件或程序集或它的一个依赖项

问题描述:

我有一个类似的错误System.BadImageFormatException: Could not load file or assembly。 但是引发错误的条件是不同的。System.BadImageFormatException:无法加载文件或程序集或它的一个依赖项

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)

条件:

  1. 该解决方案具有
  2. 的InternalsVisibleTo从控制台应用程序启用单位项目C#控制台应用程序和它的单元测试项目
  3. 所有项目都stronge名义签署
  4. sln平台有两种配置:x64和x86(其他都被删除)

当我运行将访问控制台应用程序内部元素的单元测试用例时,测试用例会失败并抛出异常。

+0

在项目中明确定位x64是无用的。总是选择AnyCPU,如果您对非托管代码有依赖性,那么只需为您的EXE项目选择x86。 –

+0

我们的系统编译为x64,这是我们的发布目标。不管怎样,谢谢你。 – Amitabha

+0

http://en.wikipedia.org/wiki/Cargo_cult_programming –

问题的根本原因是我使用x64平台来构建sln并使用x86测试设置运行测试用例。

只要使用正确的测试设置平台来运行测试用例:

  • 构建平台应该是相同的平台,运行测试用例。

截图: enter image description here

+0

哦,男人,你救了我的一天,Visual Studio的测试设置很糟糕... –

+0

我做了一个干净的构建,然后构建和错误消失 – PUG

在VS2010中,它应该是

测试 - 编辑测试设置 - 本地 - 主机

还,您需要检查生成 - 配置管理器