7

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

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

ASPNETCOMPILER 无法加载文件或程序集“AssemblyB”或其依赖项之一。试图加载格式不正确的程序。

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

我尝试引用 in 的每个依赖引用AssemblyBAssemblyA确保所有依赖引用的版本相同。

我用过这个问题:如何确定 .NET 程序集是为 x86 还是 x64 构建的?确认所有引用的程序集都是MSILAMD64

我使用了启用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 位平台目标,这不适合我的情况:

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

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

如何修复此编译错误?

4

1 回答 1

5

该指令<AspNetCompiler />接受一个ToolPath可定向到 64 位版本的参数aspnet_compiler.exe。这在这篇博文中有描述。它的要点,编辑你的 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>
于 2018-11-07T11:41:40.273 回答