1

I've got a .NET app that I have been developing for many years (about 15 projects, some of which are pretty large) and I have just encountered the following exception when trying to launch it.

System.BadImageFormatException was unhandled Message: Could not load file or assembly 'System.Runtime.Remoting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. An attempt was made to load a program with an incorrect format.

There doesn't appear to be any call stack - this happens before launching into my code, so I can't even step in. I was using this DLL in some projects, but I have now gone through and completely removed all references to it, but I am still getting the error. It is only happening with this project (I can launch other projects within the solution ok and other projects from other solutions ok).

EDIT

After a lot of fiddling around, disabling the application framework in the application tab under My Project seems to allow it to launch, but I can't work out why. I do want to re-enable it as I make use of some of those features.

EDIT 2 After a lot more messing about, I think that it is caused by the application framework using remoting to enforce that it is a single instance application. However, .NET reflector seems to indicate that this shouldn't actually be run if I uncheck the single instance option but I still have the problem even if I do. That said, it does still go into the scope of the method which uses types from that assembly (Microsoft.VisualBasic.ApplicationServices.ApplicationBase.Run), so I presume that is what is causing it to try to resolve that assembly. Whilst all this is interesting, it doesn't really give any answer as to why I'm getting this exception. I've tried reproducing it with an empty project and that works fine - both set on the same application framework settings, both on .NET 4.0, both x86. I'm pretty stumped as to where to go from here.

wizzardmr42
  • 1,634
  • 12
  • 22
  • 32 / 64 problem usually - e.g. first link http://stackoverflow.com/questions/2728560/badimageformatexception-when-loading-32-bit-dll-target-is-x86 – NSGaga-mostly-inactive Apr 05 '13 at 12:36
  • Maybe, but if I'm not even referencing it then how do I resolve!? – wizzardmr42 Apr 05 '13 at 12:46
  • you have something that depends on that - it's hard to say - it could mean 'anything' :) – NSGaga-mostly-inactive Apr 05 '13 at 12:48
  • Any ideas for tracking it down? – wizzardmr42 Apr 05 '13 at 12:54
  • try dependency walker - or implement the `AppDomain.CurrentDomain.AssemblyResolve` - e.g. something similar I posted yesterday (see the event part only) - http://stackoverflow.com/a/15824128/417747 - or some other utility to trace what's that you're actually loading – NSGaga-mostly-inactive Apr 05 '13 at 13:03
  • Tried dependency walker - not listed. Can't attach a handler for AssemblyResolve as it happens before I can get any of my code in. Not listed in .NET Reflector as a dependency either and fuslogvw isn't showing any errors – wizzardmr42 Apr 05 '13 at 13:17
  • 'Resolver' needs to be attached as early as possible - before any of your code is in scope - Main or winform setting form part, WPF before App is startup etc. It's point is to resolve assemblies - not always work - and may not in your case (if it's more rooted err). – NSGaga-mostly-inactive Apr 05 '13 at 13:21
  • Managed to attach the handler in Application constructor and enabled debugger step through on it to check it got that far. The handler isn't being called though. I have established that it makes it through the Application constructor ok though, but doesn't seem to make it as far as any of the events in the application class or even into any overridden On-methods that I try putting in – wizzardmr42 Apr 05 '13 at 13:35
  • it's pretty specific issue (I have no idea how VB application services work) - did you try debug/release if difference - how do you build ? try setting all to Any - or all to x86 (I mean all projects in configuration), that is sometimes the issue. What's your machine 86 or – NSGaga-mostly-inactive Apr 05 '13 at 14:17
  • Machine is x64. Building x86. AnyCPU/x64 build causes BadImageFormat on other references (I suppose I could try rebuilding them all as AnyCPU, but would be a pain as there are quite a few and some other .NET versions so need to redo refs). Building and debugging using VS2012. Release actually seems to get past it, but I normally always build Debug. Doesn't make any difference if I run directly or through debugger. – wizzardmr42 Apr 05 '13 at 14:41
  • it may be sometthing w/ that configuration - if Release works, it might have different setup in configurations (AnyCPU etc.). Go through that make sure is the same - make consistent. Something is still 'building' in an unexpected way - or there is a dependency that's called which isn't 'of this world':) – NSGaga-mostly-inactive Apr 05 '13 at 16:30

1 Answers1

0

It can typically occur when you changed the target framework of .csproj and reverted it back to what you started with.

Make sure 1 if supportedRuntime version="a different runtime from cs project target" under startup tag in app.config.

Make sure 2 That also means checking other autogenerated or other files in may be properties folder to see if there is no more runtime mismatch between these files and one that is defined in .csproj file.

These might just save you lot of time before you start trying different things with project properties to overcome the error.

purvin
  • 144
  • 1
  • 3