AspNetCompiler - 尝试加载具有不正确格式的程序

9
我正在构建一个只能使用64位的Web应用程序(AssemblyA),并引用另一个我构建的也只能使用64位的应用程序(AssemblyB)。
当我在AssemblyA中添加AssemblyB作为引用时,在Visual Studio中会出现以下编译错误:

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

我的两个应用程序的平台目标设置都设置为x64,目标框架设置都是.Net Framework 4.6,且Prefer 32bit未选中。
我已经尝试将AssemblyB的每个依赖引用都引用到AssemblyA中,并确保所有依赖引用的版本都相同。
我已经使用此问题:如何确定.NET程序集是为x86还是x64构建的? 确认所有引用的程序集都是MSILAMD64
我已经使用启用了errorstack选项的aspnet_compiler.exe命令行工具,并获得了以下堆栈跟踪:
[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 pection, 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, Evidence assemblySecurity, 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 preStar tInitListPath, Boolean& isRefAssemblyLoaded)中:
在System.Web.Compilation.BuildManager.ExecutePreAppStart()中:
在System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, Host ingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)中:
我已经查看了相关的问题,但都没有回答这个问题。大多数与IIS配置有关,但这是编译错误,或者解决方案是将项目设置为允许32位平台目标,这对我的情况不适用。

我不知道该怎么办。再次强调,我不想将我的任何项目设置为32位,并且这不是IIS配置问题,因为这是编译错误。

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

我该如何解决这个编译错误?

1个回答

7
指令<AspNetCompiler />有一个参数ToolPath,可以指向64位版本的aspnet_compiler.exe。这在这篇博文中有详细介绍。简而言之,编辑您的csproj文件:
AspNetCompiler节点中添加ToolPath属性:
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接