61

All of a sudden my website is not loading and giving me below error. I am running VS2017 with .Net Framework 4.7.1 on Windows 10 Home.

[BadImageFormatException: Cannot load a reference assembly for execution.]

[BadImageFormatException: Could not load file or assembly 'System.IO.Compression.ZipFile' 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.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +225
   System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +110
   System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +22
   System.Reflection.Assembly.Load(String assemblyString) +34
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +48

[ConfigurationErrorsException: Could not load file or assembly 'System.IO.Compression.ZipFile' 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) +729
   System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +247
   System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +157
   System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +226
   System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +73
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +321
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +170
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +734

[HttpException (0x80004005): Could not load file or assembly 'System.IO.Compression.ZipFile' 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) +525
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +118
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +708

Any suggestions?

8
  • Assuming this is an Acumatica website, did you recently upgrade it? Do you get this error as soon as you try to access the site in your browser?
    – Brendan
    Commented Feb 9, 2018 at 15:56
  • I installed new instance of 2018 R1 and yes as soon as I try to access the site, I am getting above error.
    – Krunal
    Commented Feb 9, 2018 at 15:59
  • Did you try to restart Windows on your machine? Sometimes these issues might be caused by pending OS/.Net updates...
    – RuslanDev
    Commented Feb 9, 2018 at 16:07
  • Restarted Windows, reinstalled .Net Framework 4.7.1 and also repaired Acumatica, nothing worked. Still on the same error. Not sure what is happening.
    – Krunal
    Commented Feb 9, 2018 at 16:59
  • 1
    Yes, I remember I tried that but after that it keeps on throwing different error one after other. I guess it was something messed up in my website web.config file which I couldn't fix it.
    – Krunal
    Commented Feb 13, 2018 at 21:29

9 Answers 9

107

What worked for me was deleting the bin and obj directories under my web app then rebuilding.

9
  • 8
    I am so depressed. I wasted an hour on this, did plenty of Clean's, but yes, you were right... deleting the obj and bin folders solved this issue for me. Many thanks. (Sigh...!) Commented Apr 25, 2018 at 10:59
  • 3
    Same for me. I did cleans, reboots NuGet restores & updates and nothing worked. I even deleted my bin folder. Apparently deleting both the bin and the obj is the trick. Commented Aug 16, 2018 at 17:18
  • 2
    Yep, this did the trick! You need to indeed delete the directories (just emptying it won't work)
    – Huy Hoang
    Commented Sep 24, 2018 at 8:25
  • 1
    Cheeze. I usually clean out all projects and reload fresh from TFS. But this time, got latest, rebuilt... and got this. Once I got rid of all obj folders - smooth sailing
    – T.S.
    Commented Jan 11, 2019 at 22:07
  • 4
    this works, but the problems comes back after I restart the app a couple time.
    – Marquez
    Commented Aug 20, 2022 at 20:03
11

There's a known issue with .NET Framework 4.7.1 in this regard. Supposedly sorted in 4.7.2, but in the meantime what can you do?

The issue is related to the serialization assemblies, which you can optionally set to generate or not as part of your build (rclick project -> Properties -> Build tab -> see 'Generate serialization assemblies' at bottom.)

What worked for me - and I'm standing on the shoulders of others in part here - is to ensure this setting is set to 'Auto'. Do a full 'clean solution', and additionally if paranoid, this PowerShell snippet is handy if run in your solution root folder:

Get-ChildItem .\ -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }

Next, add the following targets to your csproj (just inside the enclosing <Project> tag:

<Target Name="RemoveDesignTimeFacadesBeforeSGen" BeforeTargets="GenerateSerializationAssemblies">
    <ItemGroup>
      <ReferencePath Remove="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Removing DesignTimeFacades from ReferencePath before running SGen." />
  </Target>
  <Target Name="ReAddDesignTimeFacadesBeforeSGen" AfterTargets="GenerateSerializationAssemblies">
    <ItemGroup>
      <ReferencePath Include="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Adding back DesignTimeFacades from ReferencePath now that SGen has run." />
  </Target>

If still no luck, explicitly set 'Generate serialization assemblies' to 'On' to force generation, and rebuild & run.

2
  • 1
    I got OP's issue in a .NET 4.7.2 project, FWIW. @VG8TS 's solution worked immediately and permanently (erase bin and obj). Commented Jul 7, 2021 at 16:57
  • 1
    This script was helpful to me, however, it deleted the bin folder inside of my webpack dependency inside of node_modules. My fault for running a script without considering this, but let me save the next person some headache. Run at your own risk. I take no responsibility for any actions it causes. Get-ChildItem .\ -Recurse | Where-Object { $_.PSIsContainer -and $_.Name -ne 'node_modules' } | ForEach-Object { Get-ChildItem $_.FullName -Include bin, obj -Recurse | ForEach-Object { Remove-Item $_.FullName -Force -Recurse } }
    – Onosa
    Commented Nov 14, 2023 at 11:48
6

Today I have installed Acumatica 2018 R1 and I have come across this issue. Removing System.IO.Compression.ZipFile from bin folder fixed the problem.

0
6

I also faced this issue multiple time, This error may cause by reference or loading issue of dll's in your bin folder. just remove .bin folder and rebuild your application It will work.

Happy coding.

1
  • Had exactly same issue with "System.IO" loading - everything worked as usual after manual cleanup of "bin" folder.
    – Yura
    Commented Mar 9, 2021 at 10:17
3

I encountered this issue when upgrading from 4.6.2 to 4.8.

Fix:

  1. Delete assembly redirects from web.config
  2. Rebuild project
  3. Double click assembly binding warning in vs to regenerate the necessary redirects
  4. Rebuild with no warnings
2
  • How do I get number three to happen? It just errors out..
    – dtthom09
    Commented Apr 19, 2023 at 22:41
  • @dtthom09 After deleting the assembly binding redirects, after you build the project there should be warnings suggesting to add redirects. In the VS Error List (not the Output window), if you double-click one of them, it should pop a dialog asking if you want to have VS generate them all for you. Wait to do this until after the project is done building.
    – Justin
    Commented Mar 21 at 22:17
2

After all the delete nbin folder/obj folder/clean/rebuild: nothing will work. The error clearly tells you the assembly with the problem. Go to your web.config file and locate that assembly. Remove it and reference it afresh

2
  • Also check your project properties==>application Tab==>Target Framework Commented Jun 24, 2022 at 10:36
  • Chances are that the target framework in project properties doesn't match the assemblies loaded at runtime Commented Jun 24, 2022 at 10:37
2

I deleted the bin and obj folders of startup project. Then re-build the solution. It worked.

1

I have some research about the error

As we can see from the error, when we refer to SOAP based projects in Aspnetcore projects, we cannot use them directly. This is due to the implementation of the platform Aspnetcore.

It could be loaded using reflection. However, it allows us to use internal types dynamically. Support may be released in later versions of Aspnetcore for licenced soap assembly etc.

Deleting the bin folder, etc. is a solution when dll versions are different X86 or 64 etc. The real problem is the SOAP protocol, WebSocket, Proxy etc implementation. They cannot be used directly with Aspnetcore project. There's patch projects for that in github

0

I had the same issue after migrating from .NET framework 4.6.2 to 4.8, and none of the recommended fixes worked for me, until I updated my project to the newest .NET Framework 4.8.1. I the proceeded with the deletion of the bin folder and it started working again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.