变得奇怪BadImageFormatException:无法加载参考程序集以执行

问题描述 投票:0回答:7

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

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.
c# .net msbuild visual-studio-2017
7个回答
10
投票

自从升级到 .Net Framework 4.7.2 后,我遇到了同样的问题。删除 bin 文件夹会使错误消失。


6
投票

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


3
投票

与其他人的建议不同,删除 bin 和 obj 对我来说不起作用。然而,我注意到在我使用的编译后的DLL所在的文件夹中,实际上有两个不同的。我使用的是

.net5.0\ref
下的那个,但是使用
.net5.0
中的那个修复了它。名为“ref”的文件夹中的 DLL 是一个参考程序集,这是有道理的。


0
投票

或者...这就是我在 MONO 中发生的事情 - 他们有一个“binary-reference-assemblies”目录 - 这是我在项目中用于 COM 引用的目录。

链接正常,但在运行时出现此错误....

对我来说应该是显而易见的,但是 - 累了 - 我花了一段时间才注意到在其他子目录中创建了更大的二进制文件 - 一旦我选择其中一个( C:\dev\mono xternal\illinker-test-is ssets\wasm\BlazingPizza.Client 位于\Release etstandard2.0\dist_framework_bin\Mono.Security.dll 在我的例子中)一切都开始工作。

我想我会发布这篇文章,以防有人像我一样疲惫不堪并犯同样的错误(已经工作了 3 个月 7x14 的 sev 1 问题......刚刚完成)


0
投票

这是一个突然突然出现的严重问题。我将api项目升级到netfx 4.7.2后遇到了这个问题。它运行得很好,在一个美好的早晨,当我想运行我的项目时,出现了错误。

这是我在查看所有其他答案后所做的。

  1. 对解决方案执行干净构建操作
  2. 转到解决方案中的每个项目文件夹,包括测试项目并删除 BIN 和 OBJ 文件夹
  3. 如果需要,请清理软件包文件夹(当您再次构建时它将恢复),以避免任何容易实现的目标。
  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
投票

迟回答总比不回答好,希望它能帮助别人。我遇到了这个问题,并且我有一个正确的根本原因。

由于 .NET 现在在许多不同的平台上运行,因此您可以开发的 API 表面与运行时使用的程序集之间存在脱节。开发时程序集是“参考程序集”。它们包含程序集公开的所有公共类型、方法等,以便您可以获得智能感知和所有类似的东西,但它们背后没有可执行代码,它们本质上只是所有元数据。

在我的特定场景中,我们将一些项目从 .NET Framework 升级到 .NET Core,并且我们有一个较旧的构建步骤,负责将一些程序集复制到输出文件夹。升级后,由于我们有一些配置错误的路径,最终复制到输出的程序集是引用程序集,因此在运行时它将失败并出现给定的错误。

有很多实际原因会导致错误的程序集最终被复制,因此很难提供具体的解决方案,但是清理构建路径中的所有内容(正如其他人上面提到的)是一个良好的开始,以确保您正在获得您期望的组件。

如果完全清理/重建对您没有帮助,请仔细查看您的输出目录。您可以使用反编译器查看失败的程序集并验证它是否是参考程序集。之后,深入挖掘构建日志(向MSBuild结构化日志查看器)以准确识别从哪里复制程序集可以让您朝着正确的方向前进。

© www.soinside.com 2019 - 2024. All rights reserved.