We have an Asp.Net MVC project that we have recently upgraded from .Net 4.6.1 to .Net 4.8. It has some dependencies on other projects in our solution that have also been upgraded. The MVC project itself uses System.IO.Compression to zip some files as does one of the other projects it depends on.
Since upgrading, we are seeing some strange behaviour with System.IO.Compression. If we build the new solution, it doesn't automatically copy System.IO.Compression.dll and System.IO.Compression.FileSystem.dll to the output bin folder. This causes a runtime exception on the lines of code that tries to do zipping…
System.IO.FileNotFoundException: 'Could not load file or assembly 'System.IO.Compression, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.'
This is unsurprising I suppose if the dll is not even copied to the bin folder.
If I set the "Copy Local" property to true on the System.IO.Compression.dll and System.IO.Compression.FileSystem.dll in the references of the MVC project, the files are copied from
"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.8" to the bin folder, but the following error is shown in the browser when the application first starts…
[BadImageFormatException: Cannot load a reference assembly for execution.]
[BadImageFormatException: Could not load file or assembly 'System.IO.Compression' 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) +232
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +113
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +23
System.Reflection.Assembly.Load(String assemblyString) +35
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +48
[ConfigurationErrorsException: Could not load file or assembly 'System.IO.Compression' 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) +767
System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +256
System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +58
System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +287
System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +69
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +137
System.Web.Compilation.BuildManager.ExecutePreAppStart() +172
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +854
[HttpException (0x80004005): Could not load file or assembly 'System.IO.Compression' 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) +532
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +111
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +724
Curiously, the previous .Net 4.6.1 version that worked, which had Nuget references to System.IO.Compression, copied the files from
"C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib" into the bin folder. If we manually copy these versions of the files into our .Net 4.8 bin folder, the application works without any exceptions.
I have tried various combinations of bindingRedirects in the web.config file, but without success.
I have been reading other StackOverflow questions that seem related, but haven't been able to a) fully understand the issue and b) know how to fix it so that we can successfully run the .Net 4.8 version and zip files.
Any help would be greatly appreciated