13

I'm getting a crazy system load issue, which I have not idea what it is. The error and the output from the

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.

Source Error:

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.

Stack Trace:

[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

When I look in fuslogvw, I get the following as the last error:

*** 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.
double-beep
  • 5,031
  • 17
  • 33
  • 41
John Fager
  • 452
  • 1
  • 4
  • 16
  • `BadImageFormatException` usually has to do with mixed dlls instruction sets (x64 vs x86), make sure all references dll's are x86 if you are building for that architecture. – Mixxiphoid Aug 19 '18 at 20:31
  • According to the error code 0x80070002, this issue may related to an architecture mismatch. If possible, please check https://stackoverflow.com/questions/11030500/assembly-binding-error-bind-result-hr-0x80070002-the-system-cannot-find-the and https://stackoverflow.com/questions/43610551/badimageformatexception-could-not-load-file-or-assembly-or-one-of-its-dependenci – Leo Liu Aug 20 '18 at 08:05
  • I've reviewed these. Unfortunately this has to do with something with Roslyn and Microsoft's stuff. It isn't something that I added, so I don't think those are very helpful resources. – John Fager Aug 22 '18 at 14:54
  • @John Thank you for mentioning Rosyln! I had just installed a nuget package that was doing some clever compilation magic so it must have been interfering. I think maybe it's not compatible with 4.7.x – Simon_Weaver Sep 12 '18 at 06:03

6 Answers6

9

I have the same issue since I upgraded to .Net Framework 4.7.2. Deleting the bin folder makes the error go away.

Chaim Tesser
  • 91
  • 1
  • 5
  • 5
    Fixed it for me as well, but keeps coming back later – marijnz0r Nov 20 '18 at 12:53
  • i got the same problem. Is there a more permanent fix than always deleting bin and obj folder? – Gonzalo May 14 '20 at 12:53
  • I got this problem after upgrading to .Net Framework 4.7.2 from 4.6.1. Deleting the entire bin folder caused problems as various external components were stored there. I tried deleting all the System.*.*.dll files and this seemed to work. I didn't change the obj folder. – SimonKravis Dec 02 '21 at 07:38
  • @SimonKravis I don't think it's a good idea to touch anything inside a bin directory. Be it removing or adding files. External dependencies should be stored with the source code and copied to the resulting directory whenever the code is being built. – lenny May 31 '22 at 13:50
6

What worked for me was deleting the bin and obj folders. Then clean and rebuild the solution

Razor
  • 143
  • 2
  • 8
3

Unlike what others have suggested, deleting bin and obj didn't work for me. However, I noticed in the folder where the compiled DLL I was using was located, there were actually two different ones. I was using the one under .net5.0\ref, but using the one in .net5.0 fixed it. Makes sense a DLL in a folder called "ref" would be a reference assembly.

TheBoxyBear
  • 371
  • 2
  • 15
0

Or ... and this is what happened to me with MONO - they have a directory of "binary-reference-assemblies" - which is what I used in my project for the COM references.

Links fine but gets this error on run ....

Should have been obvious to me but - tired - took me a while to notice much larger binaries had been created in other sub-directories - once I select one of these ( C:\dev\mono\external\illinker-test-is\assets\wasm\BlazingPizza.Client\bin\Release\netstandard2.0\dist_framework_bin\Mono.Security.dll in my case ) everything started working.

Thought I would post this in case anyone is as bone head tired as me and makes the same mistake (been working a sev 1 issue 7x14 for 3 months...just finished it)

Mark Z. Kumler
  • 231
  • 2
  • 4
0

this is seriously an issue that comes out of nowhere all of a sudden. I got this problem after upgrading the api project to netfx 4.7.2. It was just working fine and one fine morning when I wanted to run my project, the error came up.

Here is what I did after looking at all other answers.

  1. Perform clean build operation for the solution
  2. Go to every project folder in your solution, including test projects and delete the BIN and the OBJ folder
  3. If required, clean up the packages folder (it will restore when you build again) just in case to avoid any low hanging fruits.
  4. Now do a Rebuild of your solution (basically builds all projects as defined in your build configuration)
  5. Run your project and it should work now.

I am assuming you know how to clean and rebuild your solution in Visual Studio. If not, there's plenty of guide available with a quick google search.

Jabez
  • 795
  • 9
  • 18
0

This is what worked for me:

  1. Remove all binaries from Release/ and Debug/ directories[1]
  2. Remove the packages/ directory
  3. Remove the .vs/ directory

Then re-build the solution.

[1] You can use the following code saved as a .bat file to remove all binaries from the solution: start for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s/q "%%d"

Leone
  • 3,258
  • 3
  • 22
  • 28