我正在构建一个仅限 64 位的 Web 应用程序(AssemblyA
)并引用我构建的另一个应用程序,它也是仅限 64 位(AssemblyB
)。
当我在 中添加AssemblyB
为引用时AssemblyA
,我在 Visual Studio 中收到以下编译错误:
ASPNETCOMPILER 无法加载文件或程序集“AssemblyB”或其依赖项之一。试图加载格式不正确的程序。
我的应用程序的两个平台目标设置都设置为x64
,目标框架设置都是.Net Framework 4.6
和Prefer 32bit is unchecked
。
我尝试引用 in 的每个依赖引用AssemblyB
,AssemblyA
确保所有依赖引用的版本相同。
我用过这个问题:如何确定 .NET 程序集是为 x86 还是 x64 构建的?确认所有引用的程序集都是MSIL
或AMD64
。
我使用了启用aspnet_compiler.exe
该选项的命令行工具errorstack
并获得了以下堆栈跟踪:
[BadImageFormatException]:无法加载文件或程序集“AssemblyB”或其依赖项之一。试图加载格式不正确的程序。
在 System.Reflection.RuntimeAssembly._nLoad(AssemblyName 文件名,字符串 codeBase,证据 assemblySecurity,RuntimeAssembly locationHint,StackCrawlMark 和 stackMark,IntPtr pPrivHostBinder,布尔 throwOnFileNotFound,布尔 forIntrospection,布尔suppressSecurityChecks)
在 System.Reflection.RuntimeAssembly.nLoad(AssemblyName 文件名,字符串代码库,证据 assemblySecurity,RuntimeAssembly locationHint,StackCrawlMark& s tackMark,IntPtr pPrivHostBinder,布尔 throwOnFileNotFound,布尔 forIntros pction,布尔suppressSecurityChecks)
在 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef,证据 assemblySecurity,RuntimeAssembly reqAssembly,StackCrawlMark &stackMark,IntPtr pPrivHostBinder,布尔 throwOnFileNotFound,布尔 forIntrospection,布尔suppressSecurityChecks)
在 System.Reflection.RuntimeAssembly.InternalLoad(字符串 assemblyString,证据 assemblySecurity,StackCrawlMark &stackMark,IntPtr pPrivHostBinder,Boolean forIntrospection)
在 System.Reflection.RuntimeAssembly.InternalLoad(字符串 assemblyString,证据 assemblySecurity,StackCrawlMark &stackMark,Boolean for Introspection)
在 System.Reflection.Assembly.Load(字符串 assemblyString)
在 System.Web.Configuration.CompilationSection.LoadAssemblyHelper(字符串 assemblyName,布尔星指令)
[ConfigurationErrorsException]:无法加载文件或程序集“AssemblyB”或其依赖项之一。试图加载格式不正确的程序。
在 System.Web.Configuration.CompilationSection.LoadAssemblyHelper(字符串 assemblyName,布尔星指令)
在 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(字符串 preStart tInitListPath,布尔值& isRefAssemblyLoaded)
在 System.Web.Compilation.BuildManager.ExecutePreAppStart()
在 System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager、IApplicationHost appHost、IConfigMapPathFactory configMapPathFactory、HostingEnvironmentParameters hostingParameters、PolicyLevel policyLevel、异常 appDomainCreationException)
我查看了以下相关问题,但没有人回答这个问题。大多数与 IIS 配置有关,情况并非如此,因为这是编译错误,或者有解决方案将项目设置为允许 32 位平台目标,这不适合我的情况:
- “尝试加载格式不正确的程序”即使平台相同(使用 IIS 配置不好)
- 无法加载文件或程序集...尝试加载格式不正确的程序(System.BadImageFormatException)(运行时错误不好)
- System.BadImageFormatException 尝试加载格式不正确的程序(使用 32 位解决方案不好)
- 无法加载文件或程序集“xxx”或其依赖项之一。尝试加载格式不正确的程序(不适合使用 32 位解决方案)
我不知道从这里去哪里。重申一下,我不想将我的任何一个项目设置为 32 位,这不是 IIS 配置的问题,因为这是一个编译错误。
我还在几台不同的机器和全新的应用程序上进行了尝试,结果相同。
如何修复此编译错误?