title: Getting strange BadImageFormatException: Cannot load a reference assembly for execution
body: <p>I'm getting a crazy system load issue, which I have not idea what it is. The error and the output from the</p>
  
  
  
  <pre class="lang-none prettyprint-override"><code>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.
  </code></pre>
  
  <p>Source Error:</p>
  
  <pre class="lang-none prettyprint-override"><code>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.
  </code></pre>
  
  <p>Stack Trace:</p>
  
  <pre class="lang-none prettyprint-override"><code>[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&amp; stackMark, IntPtr pPrivHostBinder, Boolean
  throwOnFileNotFound, Boolean forIntrospection, Boolean
  suppressSecurityChecks) +0   
  System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String
  codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint,
  StackCrawlMark&amp; stackMark, IntPtr pPrivHostBinder, Boolean
  throwOnFileNotFound, Boolean forIntrospection, Boolean
  suppressSecurityChecks) +36   
  System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName
  assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly,
  StackCrawlMark&amp; stackMark, IntPtr pPrivHostBinder, Boolean
  throwOnFileNotFound, Boolean forIntrospection, Boolean
  suppressSecurityChecks) +152   
  System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString,
  Evidence assemblySecurity, StackCrawlMark&amp; stackMark, IntPtr
  pPrivHostBinder, Boolean forIntrospection) +77   
  System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString,
  Evidence assemblySecurity, StackCrawlMark&amp; 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&amp; 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
  </code></pre>
  
  <p>When I look in fuslogvw, I get the following as the last error:</p>
  
  <pre class="lang-none prettyprint-override"><code>*** 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.
  </code></pre>