BadImageFormatException: Could not load file or assembly 'System.Runtime' or one of its dependencies.

May 4, 2015 at 2:37 AM
Hello,
I came across this nuget package and was very interested, I added the package and followed the instructions to create the Configuration class and added the code provided. I did a full rebuild, when I try to debug my web application get the following error, when I remove the nuget package the error goes away. Am I doing something wrong here? I'm using VS 2013 with Update 4, all of my projects are set to AnyCpu, please advise.

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

[BadImageFormatException: Could not load file or assembly 'System.Runtime' 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) +119

[ConfigurationErrorsException: Could not load file or assembly 'System.Runtime' 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) +1086
System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +360
System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +103
System.Web.Configuration.AssemblyInfo.get_AssemblyInternal() +62
System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +265
System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +77
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +173
System.Web.Compilation.BuildManager.ExecutePreAppStart() +236
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +1106

[HttpException (0x80004005): Could not load file or assembly 'System.Runtime' 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) +762
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +150
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +245
May 5, 2015 at 2:02 AM
Hello again,
I just noticed that its been more than 6 months since this package was updated last, but I did see that questions have recently been asked and answered. Is this project still being supported? Would it be more relevant to open an issue item instead of a discussion? The reason I went with a discussion is that I was following what others have done.

On a separate note I have made some progress, I noticed that after I rebuilt the solution for the first time after adding this Nuget package that something was modifying my web.config file in my presentation layer. What's interesting about this is that I did not add this Nuget package to my presentation project, I try to adhere to the separation of responsibilities principle as must as possible, with that said I have the following projects in my solution.

Projects in Alphabetical Order that are relevant to this discussion:
  • Lcms.Domain -This project contains all of the classes that are common/used by more than one project, also this project is where my code generated entity - framework classes are create when my Edmx file gets updated.
  • Lcms.Presentation.Web - This project is my presentation layer where I noticed the web.config was marked as edited after adding this Nuget package
  • Lcms.Services - This project is my data access layer and where my EntityFramework + Edmx file exists, this is also the project that I installed this Nuget
                                        package to and created the public Configuration class that was recommended to use this package.
    
I did manage to get past this error by installing this Nuget package, rebuilding my solution, then finally going back to the web.config that was being marked as modified and using TFS to undo pending changes.

Even though I was able to use the work around I mentioned I'm still hoping that someone here could shed some light on why this package need to make changes to my presentation layers web.config which again was not the project that I installed this Nuget package to, and finally why it was causing the BadImageFormatException. My best guess is that this component itself or a reference it takes was explicitly using x86 instead of AnyCpu which would explain what was going on here. After I got my application to run with your component and the work around I mentioned above I noticed a significant performance increase!!!! We use Glimpse to measure the applications performance because we have noticed some bottle necks and poor performance. We also have some basic caching of domain data as well but it did not give anywhere near the impact that your package did. On average each page load was taking being 3 and 4 seconds to complete, with your component we have seen it as low as 700MS!!!! with an average of 1 second to 1.5 seconds. This is a huge reduction it processing time all thanks to this great component! I'm really hoping that someone where could join my discussion here to help me understand why adding this component results in edits to web.config file which is in a different project than the one I installed the package to, and finally i'd like to understand why this change to the web.config cause the BadImageFormatException. My best guess is that the component itself or a reference that it takes is set to 86 instead of AnyCpu which based on my experience is usually what causes this exception.

Thanks,

Patrick
Coordinator
May 7, 2015 at 1:40 PM
What are the changes to the web.config file?