.net 异常:无法加载引用程序集以供执行

roqulrg3  于 2023-11-20  发布在  .NET
关注(0)|答案(7)|浏览(128)

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

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.

n6lpvg4x

n6lpvg4x1#

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

ktecyv1j

ktecyv1j2#

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

eyh26e7m

eyh26e7m3#

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

6qfn3psc

6qfn3psc4#

或者.这就是我在MONO中遇到的情况-他们有一个“binary-reference-assemblies”目录-这就是我在项目中用于COM引用的目录。
链接正常,但在运行时出现此错误...
对我来说应该是显而易见的,但是-累了-花了我一段时间才注意到在其他子目录中创建了更大的二进制文件-一旦我选择了其中之一(在我的情况下是C:\dev\mono\external\illinker-test-is\assets\wasm\BlazingPizza.Client\bin\Release\netstandard2.0\dist_framework_bin\Mono.Security.dll),一切都开始工作。
我想我会把这个贴出来,以防有人和我一样累,犯同样的错误(已经工作了3个月的sev 1问题7 x14...刚刚完成)

t8e9dugd

t8e9dugd5#

这是一个突然冒出来的严重问题。我在将API项目升级到NetFX 4. 7. 2后遇到了这个问题。它只是工作正常,一个晴朗的早上,当我想运行我的项目时,错误出现了。
这是我在看了所有其他答案后做的。
1.为解决方案执行干净生成操作
1.转到解决方案中的每个项目文件夹(包括测试项目),然后删除BIN和OBJ文件夹
1.如果需要,清理packages文件夹(当您再次构建时,它将恢复),以防万一,以避免任何低挂水果。
1.现在对您的解决方案进行重建(基本上按照您的构建配置中定义的那样构建所有项目)
1.运行您的项目,它现在应该工作。

  • 我假设你知道如何在Visual Studio中清理和重建你的解决方案。如果没有,有很多指南可以快速谷歌搜索。*
91zkwejq

91zkwejq6#

这就是我的工作:
1.删除Release/Debug/目录中的所有二进制文件[1]
1.删除packages/目录
1.删除.vs/目录
然后重新构建解决方案。
[1]您可以使用保存为.bat文件的以下代码从解决方案中删除所有二进制文件:start for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s/q "%%d"

xqk2d5yq

xqk2d5yq7#

迟回答总比不回答好,希望能帮助到别人。我遇到了这个问题,我有一个适当的根本原因。
因为.NET现在运行在许多不同的平台上,所以在你可以开发的API表面和运行时使用的程序集之间存在脱节。开发时程序集是“引用程序集”。它们包含程序集公开的所有公共类型,方法等,这样你就可以获得智能感和所有类似的东西,但是它们后面没有可执行代码,它们基本上都是元数据。
在我的特定场景中,我们正在将一些项目从.NET Framework升级到.NET Core,并且我们有一个较旧的构建步骤,负责将一些程序集复制到输出文件夹。升级后,由于我们有一些错误配置的路径,最终复制到输出的程序集是引用程序集,因此在运行时它会失败并给出错误。
错误的程序集最终可能被复制的实际原因有很多,所以很难提供一个特定的解决方案,但是清理构建路径中的所有内容(正如上面提到的其他人)是一个很好的开始,以确保您获得您期望的程序集。
如果完全清理/重建对您没有帮助,那么请仔细查看您的输出目录。您可以使用反编译器查看失败的程序集并验证它是引用程序集。之后,通过挖掘构建日志(大声呼叫MSBuild Structured Log Viewer)来确定该程序集被复制的确切位置可以让您朝着正确的方向前进。

相关问题