I've been chasing this problem for a few days now. I've developed a .NET 5 solution which works perfectly on my develop laptop. It consists of 3 projects (2 class libraries and a wpf project). Deployment is made via a Visual Studio 2022 installer project.
One of the class libraries references some third-party 64 bit dlls for which I have no control. Deployment target on all projects and installer project is x64 (even the "TargetPlatform" property on the installer project).
The program behaves as expected on Windows 10 Desktop OS (my dev laptop and 2 other co-workers laptops), however, when installing the program on a Windows Server 2019, I get the following exception (with xyz.123.abc being one of the third-party dlls):
System.BadImageFormatException: Could not load file or assembly 'xyz.123.abc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.
Note that the exception message does not include the "or one of its dependencies" part that I've read on other questions.
As I have no direct access to the deployment server, I set up my own Windows Server 2019 instance (with the same conditions as the deployment server) on VirtualBox and was able to reproduce the problem. It also reproduced on a Windows 11 Desktop OS.
The third-party dlls are exposed via the installation of a Program, which for testing purposes I also installed on my VM OS, but still getting the same problem.
I've made sure the .NET 5 desktop runtime is 5.0.17 on both dev, VM and deployment PCs. All environments are 64 bits as well.
As a side note, all other features of my program work as expected on all environments.
1st edit: I was able to reproduce on my dev PC by changing target platform to x86... Expected behavior since the third-party dlls are 64 bit, makes no sense to fail at runtime x64 still.
If anyone runs into this problem, check if your 3rd party dlls rely on VC++ redistributables.
In my case, I was missing VC++ 2013 redist. Which I downloaded from Microsoft directly.
Managed to find out using Dependencies