获取奇怪的BadImageFormatException错误:无法加载执行引用程序集

13

我遇到了一个疯狂的系统负载问题,完全不知道是什么。这个错误和输出来自于

Server Error in '/' Application. Cannot load a reference assembly for
execution. Description: An unhandled exception occurred during the
execution of the current web request. Please review the stack trace
for more information about the error and where it originated in the
code.

Exception Details: System.BadImageFormatException: Cannot load a
reference assembly for execution.

源代码错误:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.

堆栈跟踪:

[BadImageFormatException: Cannot load a reference assembly for
execution.]

[BadImageFormatException: Could not load file or assembly 'System.IO'
or one of its dependencies. Reference assemblies should not be loaded
for execution.  They can only be loaded in the Reflection-only loader
context. (Exception from HRESULT: 0x80131058)]   
System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String
codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint,
StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean
throwOnFileNotFound, Boolean forIntrospection, Boolean
suppressSecurityChecks) +0   
System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String
codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint,
StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean
throwOnFileNotFound, Boolean forIntrospection, Boolean
suppressSecurityChecks) +36   
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName
assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly,
StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean
throwOnFileNotFound, Boolean forIntrospection, Boolean
suppressSecurityChecks) +152   
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString,
Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr
pPrivHostBinder, Boolean forIntrospection) +77   
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString,
Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean
forIntrospection) +21    System.Reflection.Assembly.Load(String
assemblyString) +28   
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String
assemblyName, Boolean starDirective) +38

[ConfigurationErrorsException: Could not load file or assembly
'System.IO' or one of its dependencies. Reference assemblies should
not be loaded for execution.  They can only be loaded in the
Reflection-only loader context. (Exception from HRESULT: 0x80131058)] 
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String
assemblyName, Boolean starDirective) +726   
System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory()
+196    System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo
ai) +45   
System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection
compConfig) +172   
System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies()
+91    System.Web.Compilation.BuildManager.CallPreStartInitMethods(String
preStartInitListPath, Boolean& isRefAssemblyLoaded) +111   
System.Web.Compilation.BuildManager.ExecutePreAppStart() +156   
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager
appManager, IApplicationHost appHost, IConfigMapPathFactory
configMapPathFactory, HostingEnvironmentParameters hostingParameters,
PolicyLevel policyLevel, Exception appDomainCreationException) +695

[HttpException (0x80004005): Could not load file or assembly
'System.IO' or one of its dependencies. Reference assemblies should
not be loaded for execution.  They can only be loaded in the
Reflection-only loader context. (Exception from HRESULT: 0x80131058)] 
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +659   
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +89
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest
wr, HttpContext context) +188


Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.7.3160.0

当我查看fuslogvw时,我得到以下内容作为最后一个错误:

*** Assembly Binder Log Entry  (8/19/18 @ 12:10:13 PM) ***

The operation failed. Bind result: hr = 0x80070002. The system cannot
find the file specified.

Assembly manager loaded from: 
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll Running under
executable  C:\Program Files (x86)\Microsoft Visual
Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\VBCSCompiler.exe
--- A detailed error log follows. 

=== Pre-bind state information === LOG: DisplayName = System.Runtime.Loader, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a  (Fully-specified) LOG: Appbase =
file:///C:/Program Files (x86)/Microsoft Visual
Studio/2017/Professional/MSBuild/15.0/Bin/Roslyn/ LOG: Initial
PrivatePath = NULL LOG: Dynamic Base = NULL LOG: Cache Base = NULL
LOG: AppName = VBCSCompiler.exe Calling assembly :
Microsoft.CodeAnalysis, Version=2.9.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35.
=== LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Program Files (x86)\Microsoft
Visual
Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\VBCSCompiler.exe.Config
LOG: Using host configuration file:  LOG: Using machine configuration
file from
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Runtime.Loader, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a LOG: GAC Lookup was
unsuccessful. LOG: Attempting download of new URL file:///C:/Program
Files (x86)/Microsoft Visual
Studio/2017/Professional/MSBuild/15.0/Bin/Roslyn/System.Runtime.Loader.DLL.
LOG: Attempting download of new URL file:///C:/Program Files
(x86)/Microsoft Visual
Studio/2017/Professional/MSBuild/15.0/Bin/Roslyn/System.Runtime.Loader/System.Runtime.Loader.DLL.
LOG: Attempting download of new URL file:///C:/Program Files
(x86)/Microsoft Visual
Studio/2017/Professional/MSBuild/15.0/Bin/Roslyn/System.Runtime.Loader.EXE.
LOG: Attempting download of new URL file:///C:/Program Files
(x86)/Microsoft Visual
Studio/2017/Professional/MSBuild/15.0/Bin/Roslyn/System.Runtime.Loader/System.Runtime.Loader.EXE.
LOG: All probing URLs attempted and failed.

BadImageFormatException通常与混合的dll指令集(x64 vs x86)有关,如果您正在为该架构构建,请确保所有引用的dll都是x86。 - Mixxiphoid
根据错误代码0x80070002,此问题可能与架构不匹配有关。如果可能,请查看https://dev59.com/YmTWa4cB1Zd3GeqPIfdT和https://dev59.com/Vek5XIcBkEYKwwoY_O13。 - Leo Liu
我已经审查过了。不幸的是,这与Roslyn和Microsoft的东西有关。这不是我添加的内容,所以我认为这些资源并没有什么帮助。 - John Fager
@John 谢谢你提到Roslyn!我刚刚安装了一个NuGet包,它正在进行一些聪明的编译魔法,所以它可能会干扰。我认为它可能与4.7.x不兼容。 - Simon_Weaver
7个回答

10
我升级到 .Net Framework 4.7.2 后出现了相同的问题。删除 bin 文件夹可以消除错误。

5
已经帮我解决了问题,但之后问题又出现了。 - marijnz0r
我遇到了同样的问题。除了始终删除 bin 和 obj 文件夹之外,是否有更持久的解决方法? - Gonzalo
我从4.6.1升级到.NET Framework 4.7.2后遇到了这个问题。删除整个bin文件夹会导致问题,因为有各种外部组件存储在里面。我尝试删除所有的System...dll文件,这似乎起作用了。我没有改变obj文件夹。 - SimonKravis
@SimonKravis 我认为在 bin 目录内部触碰任何东西都不是一个好主意,无论是删除还是添加文件。外部依赖应该与源代码一起存储,并在构建代码时复制到结果目录中。 - lenny

6

对我有用的方法是删除bin和obj文件夹,然后清理并重新构建解决方案。


3
与其他人建议的不同,删除bin和obj对我没有用。然而,我注意到在我使用的编译后DLL所在的文件夹中实际上有两个不同的DLL。我正在使用.net5.0\ref下的一个,但是使用.net5.0下的一个解决了问题。在名为“ref”的文件夹中的DLL肯定是参考程序集,这很有道理。

0

这是一个突然出现的严重问题。我在将API项目升级到netfx 4.7.2后遇到了这个问题。它之前一直正常工作,但有一天早上当我想运行我的项目时,就出现了错误。

在查看所有其他答案后,我做了以下操作:

  1. 对解决方案执行清理构建操作
  2. 进入解决方案中的每个项目文件夹,包括测试项目,并删除BIN和OBJ文件夹
  3. 如果需要,清理packages文件夹(再次构建时会还原),以防止出现任何低悬果实。
  4. 现在重新构建您的解决方案(基本上按照构建配置中定义的所有项目进行构建)
  5. 运行您的项目,现在应该可以正常工作了。

我假设您知道如何在Visual Studio中清理和重新构建解决方案。如果不知道,可以通过快速谷歌搜索找到大量指南。


0

这是对我有效的方法:

  1. Release/Debug/ 目录中删除所有二进制文件[1]
  2. 删除 packages/ 目录
  3. 删除 .vs/ 目录

然后重新构建解决方案。

[1] 您可以使用以下代码保存为 .bat 文件,以从解决方案中删除所有二进制文件: start for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s/q "%%d"


0

或者...这就是我在使用MONO时遇到的问题-他们有一个“二进制参考程序集”目录-这就是我在项目中用于COM引用的内容。

链接很好,但在运行时出现了这个错误....

对我来说应该很明显,但是因为疲劳,花了一段时间才注意到其他子目录中已经创建了更大的二进制文件-一旦我选择了其中一个(例如我的情况是C:\ dev \ mono \ external \ illinker-test-is \ assets \ wasm \ BlazingPizza.Client \ bin \ Release \ netstandard2.0 \ dist_framework_bin \ Mono.Security.dll),一切都开始正常工作。

我想发帖是因为如果有人像我一样犯了同样的错误(已经连续3个月以7x14的方式处理sev 1问题...刚刚完成),那么这篇文章可能会有所帮助。


0
希望这个答案能帮到某些人。我遇到了这个问题,并找到了一个合适的根本原因。
由于.NET现在在许多不同的平台上运行,开发者可以开发的API表面与运行时使用的程序集之间存在断开。开发时使用的程序集是“引用程序集”。它们包含了程序集所公开的所有公共类型、方法等,以便您可以获得智能感知和其他相关功能,但实际上它们没有可执行代码,它们只是元数据。
在我的具体情况中,我们正在将一些项目从.NET Framework升级到.NET Core,并且我们有一个旧的构建步骤负责将一些程序集复制到输出文件夹。升级后,由于一些配置错误的路径,最终被复制到输出文件夹的是引用程序集,因此在运行时会出现给定的错误。
实际上,错误程序集被复制的原因有很多,所以很难提供具体的解决方案,但是清理构建路径中的所有内容(正如其他人在上面提到的)是一个好的起点,以确保您获得预期的程序集。
如果完全清理/重建没有帮助,那么请仔细查看您的输出目录。您可以使用反编译器查看失败的程序集,并验证它是否是一个引用程序集。之后,通过查看构建日志(向MSBuild Structured Log Viewer致敬)来确定该程序集是从哪里复制过来的,这样可以帮助您朝正确的方向前进。

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