Cannot load a reference assembly for execution

41

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?

asp.net
visual-studio
visual-studio-2017
acumatica
asked on Stack Overflow Feb 9, 2018 by Krunal • edited Nov 27, 2018 by svick

6 Answers

64

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

answered on Stack Overflow Apr 18, 2018 by VG8TS
8

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.

answered on Stack Overflow Mar 22, 2018 by oflahero • edited Jul 28, 2019 by Dave
5

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.

3

There is no solution to this except creating a new instance and migrate all your customization. Such errors are just random and difficult to trace or solve.

answered on Stack Overflow Mar 22, 2018 by Krunal
2

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.

answered on Stack Overflow May 4, 2020 by rohit panchal
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

answered on Stack Overflow Jul 23, 2019 by Hamit YILDIRIM • edited Jul 23, 2019 by Hamit YILDIRIM

User contributions licensed under CC BY-SA 3.0