I have a Nancy project on .NET 4.8 in C# which was giving the error below on this line:
host = new NancyHost(new HttpBootstrapper(), hostConfigs, uri);
System.BadImageFormatException: Could not load file or assembly [name of assembly] The module was expected to contain an assembly manifest
The assembly referenced is a 3rd-party 32-bit C DLL which one of my C# DLL's was referencing. All other projects using this DLL worked fine. Just the Nancy Project was not happy.
Now this has happened before and I had forgotten how to solve it. Thus, I'm writing and answering my own post on this. It's a hard error to solve because BadImageFormatException happens all over the place for many very different reasons.
Here is the solution that apparently I've been using for years, which only crops up when I reset my Exception settings in Visual Studio: simply Ignore those exceptions!
Just clear the checkbox "Break when this exception type is thrown". Everything will work perfectly. I have no idea what the underlying issue is, no idea if the problem is important or not. But this solution works perfectly find and lets me get to the 10,000 other issues that I need to solve on a daily basis.
I think it's related to old C DLLS being called by C#. It would be interesting to understand how to fix it, but that is not mission critical.