Could not load file or assembly... Windows Azure Website

10

I understand there is lots of these posts around and I've dealt with these on my own before with no problem except this time. It's because I can't get the debug information i need out of Windows Azure and hoping someone might be able to help me out.

This all work's perfectly fine in my local environment, debug & release.

I'm getting this error: Could not load file or assembly 'Lib' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Lib does exist in the directory, it depends on only System.Drawing & mscorlib.

Usually I would just attached to AppDomain.Current.AssemblyResolve or check the exception when it's throw. I'm not able to do this with Azure because the exception happens during the preload stuff.

    [BadImageFormatException: Could not load file or assembly 'Lib' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   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 'Lib' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +736
   System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +217
   System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +130
   System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +170
   System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +91
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +284
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +153
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +521

[HttpException (0x80004005): Could not load file or assembly 'Lib' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9930568
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

Is there a way to override / create custom assembly loader to intercept this problem?

I'm at a complete loss.

Thanks

Steve

c#
.net
azure-web-sites
badimageformatexception
asked on Stack Overflow Aug 28, 2014 by Steven Yates

2 Answers

16

I hate it when i do this. Post a question on SO because I've been trying all day, then i fix it 10 minutes after.

So it seem's i missed a piece of key information which would of helped a lot.

BadImageFormatException

This (as far as i can tell) is thrown when there are problems with the PLATFORM (architecture) compatibility (x86, x64). All of my projects were compiled for "Any CPU" (can be found under project properties > Build > Platform Target, VS2013).

However my "Lib" project was built for x64 only, and the Azure Web Site was running in 32bit mode so was not able to load the 64 bit dll.

Two options:

  1. Compile the "Lib" dll as AnyCPU or 32bit then re-publish
  2. Switch the azure web site to 64 bit.

I wen't with option 2 because of what the "Lib" dll does I need it as 64 bit.

So for future reference if anyone else has something like this, check the following:

  1. Azure website Platform (found under Configure> Platform, on the old portal)
  2. Check all your project's are set for any CPU or a compatible "Platform (architecture)"

I hope this helps someone else.

Thanks

Steve

EDIT: If anyone else has some more useful information to add for people in the future which might have this problem, please do.

answered on Stack Overflow Aug 28, 2014 by Steven Yates
0

I know the question says something about "Lib" which was not my case. However I had the BadImageFormatException in an Azure App Service and this was the closest question I found. Turns out I deployed the app with the appType webAppLinux and not webAppWindows (pipeline deployment) and agent-based monitoring Application Insights was enabled and that it's not supported in Linux. So for that kind of problem, turning off the agent-based monitoring solved my problem. You need to restart the app after turning it off by the way.

answered on Stack Overflow Jun 20, 2020 by Juan Carlos Puerto

User contributions licensed under CC BY-SA 3.0