【发布时间】:2016-05-17 15:17:06
【问题描述】:
我正在构建一个仅限 64 位的 Web 应用程序 (AssemblyA) 并引用我构建的另一个仅限 64 位的应用程序 (AssemblyB)。
当我在 AssemblyA 中添加 AssemblyB 作为引用时,我在 Visual Studio 中收到以下编译错误:
ASPNETCOMPILER 无法加载文件或程序集“AssemblyB”或其依赖项之一。试图加载格式不正确的程序。
我的应用程序的两个平台目标设置都设置为x64,两个目标框架设置都是.Net Framework 4.6 和Prefer 32bit is unchecked。
我已经尝试在AssemblyA 中引用AssemblyB 的每个依赖引用,确保所有依赖引用的版本相同。
我使用了这个问题:How to determine if a .NET assembly was built for x86 or x64? 来确认所有引用的程序集是 MSIL 或 AMD64。
我使用了aspnet_compiler.exe 命令行工具并启用了errorstack 选项并获得了以下堆栈跟踪:
[BadImageFormatException]:无法加载文件或程序集“AssemblyB”或其依赖项之一。试图加载格式不正确的程序。
在 System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntro spection, Boolean suppressSecurityChecks)
在 System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String code Base, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& s tackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntros pction, Boolean suppressSecurityChecks)
在 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark &stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
在 System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, 证据汇编Security, StackCrawlMark &stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
在 System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark &stackMark, Boolean forIntrospection)
在 System.Reflection.Assembly.Load(String assemblyString)
在 System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective)
[ConfigurationErrorsException]:无法加载文件或程序集“AssemblyB”或其依赖项之一。试图加载格式不正确的程序。
在 System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective)
在 System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomain BinDirectory()
在 System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai)
在 System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig)
在 System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies()
在 System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStart tInitListPath, Boolean& isRefAssemblyLoaded)
在 System.Web.Compilation.BuildManager.ExecutePreAppStart()
在 System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)
我查看了以下相关问题,但没有人回答这个问题。大多数与IIS配置有关,但不是这种情况,因为这是编译错误,或者有解决方案将项目设置为允许32位平台目标,这不适合我的情况:
- "An attempt was made to load a program with an incorrect format" even when the platforms are the same(不如使用 IIS 配置)
- Could not load file or assembly ... An attempt was made to load a program with an incorrect format (System.BadImageFormatException)(没有运行时错误)
- System.BadImageFormatException An attempt was made to load a program with an incorrect format(不如使用 32 位解决方案)
- Could not load file or assembly 'xxx' or one of its dependencies. An attempt was made to load a program with an incorrect format(不如使用 32 位解决方案)
我不知道从这里去哪里。重申一下,我不想将我的任何一个项目设置为 32 位,这不是 IIS 配置的问题,因为这是一个编译错误。
我还在几台不同的机器和全新的应用程序上进行了尝试,结果相同。
如何解决这个编译错误?
【问题讨论】:
标签: c# .net compilation compiler-errors badimageformatexception