【问题标题】:Getting strange BadImageFormatException: Cannot load a reference assembly for execution变得奇怪 BadImageFormatException:无法加载参考程序集以执行
【发布时间】:2019-01-25 23:49:52
【问题描述】:

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

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 与 x86),如果您正在为该架构构建,请确保所有引用 dll 都是 x86。
  • 根据错误代码0x80070002,此问题可能与架构不匹配有关。如果可能,请查看stackoverflow.com/questions/11030500/…stackoverflow.com/questions/43610551/…
  • 我已经查看了这些。不幸的是,这与 Roslyn 和微软的东西有关。这不是我添加的,所以我认为这些资源不是很有帮助。
  • @John 感谢您提及 Rosyln!我刚刚安装了一个 nuget 包,它正在做一些聪明的编译魔法,所以它一定是在干扰。我想也许它与 4.7.x 不兼容

标签: c# .net msbuild visual-studio-2017


【解决方案1】:

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

【讨论】:

  • 也为我修复了它,但稍后会回来
  • 我遇到了同样的问题。有没有比总是删除 bin 和 obj 文件夹更永久的修复方法?
  • 我从 4.6.1 升级到 .Net Framework 4.7.2 后遇到了这个问题。删除整个 bin 文件夹会导致问题,因为其中存储了各种外部组件。我尝试删除所有 System.*.*.dll 文件,这似乎有效。我没有更改 obj 文件夹。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    与其他人的建议不同,删除 bin 和 obj 对我不起作用。但是,我注意到在我使用的已编译 DLL 所在的文件夹中,实际上有两个不同的。我使用的是.net5.0\ref 下的那个,但使用.net5.0 下的那个修复了它。有意义的是,名为“ref”的文件夹中的 DLL 将是一个引用程序集。

    【讨论】:

      【解决方案4】:

      或者...这就是我在使用 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 问题...刚刚完成)

      【讨论】:

        【解决方案5】:

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

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

        1. 为解决方案执行干净的构建操作
        2. 转到解决方案中的每个项目文件夹,包括测试项目并删除 BIN 和 OBJ 文件夹
        3. 如果需要,请清理 packages 文件夹(当您再次构建时它会恢复)以防万一,以避免任何低垂的果实。
        4. 现在重新构建您的解决方案(基本上构建您构建配置中定义的所有项目)
        5. 运行您的项目,它现在应该可以运行了。

        我假设您知道如何在 Visual Studio 中清理和重建您的解决方案。如果没有,可以通过快速谷歌搜索获得大量指南。

        【讨论】:

          【解决方案6】:

          这对我有用:

          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"

          【讨论】:

            猜你喜欢
            • 2023-03-22
            • 2018-02-08
            • 2023-03-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-11
            • 2017-12-14
            • 2018-12-15
            相关资源
            最近更新 更多