c# - AspNetCompiler - 尝试加载格式不正确的程序

标签 c# .net compilation compiler-errors badimageformatexception

我正在构建一个仅 64 位的 Web 应用程序 (AssemblyA) 并引用我构建的另一个应用程序,该应用程序也是仅 64 位的 (AssemblyB)。

当我将 AssemblyB 添加为 AssemblyA 中的引用时,我在 Visual Studio 中收到以下编译错误:

ASPNETCOMPILER Could not load file or assembly 'AssemblyB' or one of its dependencies. An attempt was made to load a program with an incorrect format.

我的应用程序的两个平台目标设置都设置为 x64,两个目标框架设置都是 .Net Framework 4.6 并且 Prefer 32bit 未选中 .

我尝试在 AssemblyA 中引用 AssemblyB 的每个依赖引用,确保所有依赖引用的版本都相同。

我用过这个问题:How to determine if a .NET assembly was built for x86 or x64?确认所有引用的程序集都是 MSILAMD64

我使用了启用了 errorstack 选项的 aspnet_compiler.exe 命令行工具并获得了以下堆栈跟踪:

[BadImageFormatException]: Could not load file or assembly 'AssemblyB' or one of its dependencies. An attempt was made to load a program with an incorrect format.

at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntro spection, Boolean suppressSecurityChecks)

at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String code Base, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& s tackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntros pection, 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.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark &stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)

at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark &stackMark, Boolean forIntrospection)

at System.Reflection.Assembly.Load(String assemblyString)

at System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective)

[ConfigurationErrorsException]: Could not load file or assembly 'AssemblyB' or one of its dependencies. An attempt was made to load a program with an incorrect format.

at System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective)

at System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomain BinDirectory()

at System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai)

at System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig)

at System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies()

at System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStar tInitListPath, Boolean& isRefAssemblyLoaded)

at System.Web.Compilation.BuildManager.ExecutePreAppStart()

at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, Host ingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)

我查看了以下相关问题,但没有人回答这个问题。大多数与 IIS 配置有关,但不是这种情况,因为这是编译错误,或者有解决方案,如将项目设置为允许 32 位平台目标,这不适合我的情况:

我不知道从这里去哪里。重申一下,我不想将我的任何一个项目设置为 32 位,这不是 IIS 配置的问题,因为这是一个编译错误。

我还在几台不同的机器和全新的应用程序上尝试过,结果相同。

如何修复此编译错误?

最佳答案

指令<AspNetCompiler />接受争论ToolPath可以定向到 64 位版本的 aspnet_compiler.exe .这在 this blogpost 中有描述。 .它的要点是,编辑您的 csproj 文件:

添加 ToolPath属性为 AspNetCompiler节点:

<AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)" ToolPath="$(AspNetToolPath)" />

哪里MvcBuildViews已定义,添加属性 FrameworkVersionAspNetToolPath ,像这样:

<MvcBuildViews>true</MvcBuildViews>
<FrameworkVersion>v4.0.30319</FrameworkVersion>
<AspNetToolPath Condition=" '$(Platform)' == 'x64'">$(windir)\Microsoft.NET\Framework64\$(FrameworkVersion)</AspNetToolPath>

关于c# - AspNetCompiler - 尝试加载格式不正确的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35261533/

相关文章:

c# - 如何在C#中向对象数组添加值

c# - 将 16 位有符号整数转换为 2 字节?

c# - 在C#中,有没有办法始终如一地获取当前焦点窗口的选定文本内容?

.net - 处理先前的 jwt token ,其中为相同的凭证和安全性生成新的 jwt token

编译错误 :/usr/bin/ld: cannot find -lclntsh

将c和lua程序编译成单个可执行文件

c# - ASP.Net 上的预编译和启动时间

c# - C# 中的变量作用域 - 使用 session 还是类?

c# - 在 C# 中使用 vb.net 类

c# - 读取\写入文件: heavily used application的正确方法