.Net 4.6 website not loading Reference Assemblies correctly

21

I have a web project in Visual Studio 2013, including several library projects.

Problem is that adding a reference (ie. System.Collection, System.Net) to the web project is being added as a 'Reference Assembly' from C:\Program Files (x86)\Reference Assemblies\Microsoft, when loaded in IIS it is not correctly loading the implementation of the assembly (from GAC). Example error follows.

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

[BadImageFormatException: Could not load file or assembly 'System.Collections' 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) +34
   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) +16
   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.Collections' 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) +728
   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) +624

[HttpException (0x80004005): Could not load file or assembly 'System.Collections' 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) +95
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +189

Deleting the reference dlls from the bin folder fixes the issue, but I am not sure what needs changing to fix this properly.

c#
asp.net
.net
iis
.net-assembly
asked on Stack Overflow Sep 24, 2015 by Sam • edited Sep 24, 2015 by peterdn

6 Answers

34

I delete the package from the /Bin folder

System.Collections and System.Collections.Concurrent

and rebuild project

its works.

answered on Stack Overflow Mar 27, 2017 by Brayan Aguilar
8

Resolution:

My library projects were referencing some core libs (System.* etc) with the RequiredTargetFramework option set to 3.5. This was only evident in the csproj file, example:

<Reference Include="System.Core">
  <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>

Thus causing all sorts of issues with .net versions, visual studio was trying to sort it by adding binding redirects into my web.config to point these to v4 (and include reference assemblies), unsuccessfully.

Removing all RequiredTargetFramework elements from the csproj files has solved the problem.

answered on Stack Overflow Sep 25, 2015 by Sam
4

Any time you see BadImageFormatException, you have an issue of binary format compatibility. May be your IIS pool configured to run 32-bit pool and your assemblies are built to x64, or wise versa. Or, may be, you trying to run x64 assemblies on 32 bit machine. May be you have x64 machine, anycpu-built assemblies but some 3rd party assembly is built strictly to 32-bit code.

It is one of these, or similar

Now, "usr" has good point here. You do have Cannot load a reference assembly for execution but in context with BadImageFormatException. I am wondering if this happens at compilation time. For this, try to add this to web.config

<compilation>
  <assemblies>
      <remove assembly="System.Collections" />
  . . . . 

Or, if you have

<add assembly="System.Collections. . . ."  />

Try removing it first

Now, it is normal that GAC is preferred location for reference unless you supply probing settings

answered on Stack Overflow Sep 24, 2015 by T.S. • edited Sep 24, 2015 by T.S.
2

It happened to me after VS 15.8 update. Setting "Copy Local" to false solved the issue for each assembly that gave me the error. In addition I removed (by hand) duplicated "<Private>" tags in .csproj.

answered on Stack Overflow Sep 9, 2018 by Felipe Moura • edited Oct 13, 2018 by Uwe Keim
1

This issue will often happen if your binding redirects refer to earlier versions than what your references depend on, and this may happen after updating packages (e.g., through NuGet). To resolve generally, I've added a series of steps as an answer here. However, for this particular issue, I recommend specifically following step 5 which is:

Remove all the assembly bindings from all app.config and Web.config files then build your solution. app.config bindings are not required anymore. Web.config bindings will be re-added in the next step, but removing them first ensures you don't have any outdated versions in your bindings.

answered on Stack Overflow Sep 19, 2018 by Neo
0

In my case, I added the nuget package System.Collections to my asp.net 4.6.1 project and, for some reason, it wasn't referenced into the csproj file. I manually edited the csproj file and added the reference. Reloaded it, and, voila, worked!

answered on Stack Overflow Dec 16, 2015 by Marco Alves

User contributions licensed under CC BY-SA 3.0