【问题标题】:AspNetCompiler - An attempt was made to load program with an incorrect formatAspNetCompiler - 试图加载格式不正确的程序
【发布时间】:2016-05-17 15:17:06
【问题描述】:

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

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

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

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

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

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

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

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

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

如何解决这个编译错误?

【问题讨论】:

    标签: c# .net compilation compiler-errors badimageformatexception


    【解决方案1】:

    指令<AspNetCompiler /> 接受一个参数ToolPath,该参数可以指向aspnet_compiler.exe 的64 位版本。这在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>
    

    【讨论】:

      猜你喜欢
      • 2014-01-11
      • 1970-01-01
      • 2012-05-01
      • 2014-02-10
      • 2015-10-13
      • 1970-01-01
      • 2012-11-02
      • 2019-01-14
      • 1970-01-01
      相关资源
      最近更新 更多