Recently I was cleaning up some code by refactoring a bunch of similar code into a common utility class library project and when I went to debug my first attempt I got the following error:
This was frustrating because all I did was take a static method in it’s entirety from one project to another, something I’ve done a thousand times before. On a whim I tried running the exact same code in Release mode and it ran just fine. So what I was trying to do was legit, I had an odd configuration mode issue.
After a little research I found the culprit, Viewing the properties on the new class library project and also the console application I found that someone on my team had changed the platform target to be x86 instead of “Any CPU”. Some of the references that I added for the class library were built for other platforms. After changing the platform target to “Any CPU” it worked just fine.
There were several only resources that I found saying to change the platform target and be done with it, in this case it worked for me however I wanted to point out one thing. As a convention I typically set the Platform for all of my projects to Any CPU so it is as portable as possible however there are cases where you cannot simply change the platform. As an example:
This is the Configuration Manager found when you right click on the Solution itself. You will notice that three of the projects in the solution have a Platform defined as x86 and the active solution platform for the Debug configuration is set to “Mixed Platforms”. In this case these three projects cannot run on x64 because of the project requirements.
So keep in mind that even though you may read elsewhere that Any CPU is always the answer you need to look at your environment and the project requirements to make sure it does work. So even though I normally use Any CPU as well it is not always the right answer for every project.