❔ System.BadImageFormatException: Cannot load a reference assembly for execution.
Hello,
I'm trying to debug this exception in my C# app.
I've tried solutions from stack overflow - ie. cleaning and rebuilding the app; as well as updating the .NET framework that this app targets. But to no results.
I'm at the point where each library needs to be checked if a library is causing this exception or if a library is completely missing.
I'm curious if others here have experienced similar runtime exceptions in the past and if there are other considerations.
Thank you for reading this post 🙂
13 Replies
You'll need to provide quite a bit more detail for anyone to help you with this
Oh ok - fair. Wasn't sure if enough or too little.
Here is the exception :
You'll need to tell us what exactly you're doing, what assembly you're trying to load, how that assembly was created, etc. We're not mind readers, we can't just magically fix things without all the details 🙂
To be honest, I'm not sure which assemblies are currently not being loaded.
I've been trying other general solutions to potentially hone down on what the issue was.
I'll be following up on that as I gather more info later on - it looks like atm I'll need to setup remote debugging (which is new to me)
This is a runtime error?
What happens if you restore all NuGet packages?
If you're using Visual Studio, the NuGet manager usually tells you if there are any versioning issues. Though, I have found it to be finicky for large solutions.
BadImageFormatException Class (System)
The exception that is thrown when the file image of a dynamic link library (DLL) or an executable program is invalid.
There's some code here for troubleshooting
It doesn't return an error when I run dotnet restore in Visual Studio.
- the version of windows I'm targeting is not the same as the one I'm building on (target Win10 LSTC - 1809 vs. Win10 22H2).
I'm building against .Net 4.8 which is supported on 1809.
I see, I didn't know there were versions of Window 10 that didn't support any version of .NET Framework 4. 🤔
There aren't
That's the weird part.
But - I have cut down the mystery today (at least getting closer).
I've tried a remote debugging session to reproduce the error on the target machine and the exception did not show up.
But the local installation has this error occur on runtime and it occurs when running a feature that involves either (System.Net.Http or System.Net.Json - I think; I could be totally wrong too...).
I think it could be a possibility that the installer could be the cause of it and that a dll dependency is not being loaded or wasn't configured or just missing.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.