Visual Studio 2022 seems to have a problem loading x86 .dlls, in my case,("Microsoft.Dynamics.BusinessConnectorNet"), in running time using the Any CPU configuration.
Could not load file or assembly 'Microsoft.Dynamics.BusinessConnectorNet' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
I understand that Visual Studio 2019 and earlier have x86 as the default for Any CPU and Visual Studio 2022 x64.
What I find very weird is that even configuring the whole solution to x86 still gives the same error, but this time in the Project instead of the x86 .dll file, while in Visual Studio 2017, the same solution is running fine.
Could not load file or assembly 'ProjectName' or one of its dependencies. An attempt was made to load a program with an incorrect format.
I have tried:
Creating an x86 & custom configuration for the whole solution.
Adding different property groups to the dll for different configurations in the C# Project File.
Forcingly removing the mismatch processor warning in the Project file.
Nothing has worked so far.
What I am expecting is what Visual Studio 2017 does as default for Any CPU to be able to run the IIS application which is working fine with the same solution.
Verify if you have Microsoft.Dynamics.BusinessConnectorNet.dll that is for 32bit or 64bit architecture. Probably now you have 64bit version and this is why it cannot be loaded.
EDIT:
As Matthew Watson writes, selecting AnyCPU means that the executable will run as 64 bit on a 64 bit architecture and 32 bit on a 32 bit architecture.
EDIT 2 (SOLUTION):
One more thing to check: verify if your project is running under IIS Express 32bit or 64bit. For web projects iisexpress.exe is your executable and all dlls have to be compatible with it. You have both IIS Express versions installed on your machine, one in %PROGRAMFILES%\IIS Express
and one in %PROGRAMFILES(x86)%\IIS Express
. On project properties (tab "Web") you can select which IIS Express version is in use.
Visual Studio 2022 is indeed using IIS x64 as default, therefore it was not possible to load the Microsoft.Dynamics.BusinessConnectorNet changing the IIS Express Bitness to x86 will fix the problem.