我正在构建一个仅 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?确认所有引用的程序集都是 MSIL
或 AMD64
。
我使用了启用了 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 位平台目标,这不适合我的情况:
- "An attempt was made to load a program with an incorrect format" even when the platforms are the same (不如使用 IIS 配置好)
- Could not load file or assembly ... An attempt was made to load a program with an incorrect format (System.BadImageFormatException) (运行时错误不好)
- System.BadImageFormatException An attempt was made to load a program with an incorrect format (不如使用 32 位解决方案)
- Could not load file or assembly 'xxx' or one of its dependencies. An attempt was made to load a program with an incorrect format (不如使用 32 位解决方案)
我不知道从这里去哪里。重申一下,我不想将我的任何一个项目设置为 32 位,这不是 IIS 配置的问题,因为这是一个编译错误。
我还在几台不同的机器和全新的应用程序上尝试过,结果相同。
如何修复此编译错误?
最佳答案
指令<AspNetCompiler />
接受争论ToolPath
可以定向到 64 位版本的 aspnet_compiler.exe
.这在 this blogpost 中有描述。 .它的要点是,编辑您的 csproj 文件:
添加 ToolPath
属性为 AspNetCompiler
节点:
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)" ToolPath="$(AspNetToolPath)" />
哪里MvcBuildViews
已定义,添加属性 FrameworkVersion
和 AspNetToolPath
,像这样:
<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/