Diyoda_ Diyoda_ - 1 month ago 12
C# Question

.NET project not building after changing the platform to x64 and back to Any CPU

I have 5 projects in my .net c# solution. I have changed each of the projects platform to x64 in Build's platform target (This was initially 'Any CPU') and the project worked fine.

Then I changed the platform in solution properties using configuration manager in solution properties (Right Click on Solution -> Properties)

Then I changed then back to Any CPU. But I can not build the solution. There are many errors saying that dlls of each project can not be found.

One thing I noticed when I changed the platform in solution properties and built the built has changed from bin/debug to bin/x64/debug (I am running in debug mode)

Ex error :

Metadata file '[project path]\bin\Debug\Thahavuru.DataAccessLayer.dll' could not be found


I am confused of what has to build the project successfully back again. Help is greatly appreciated.

Answer

Tinkering with the solution's Platform selection is always wrong. It is a setting that only matters to C++ projects. Managed projects get compiled to assemblies that contain MSIL, they run on any platform. It is the jitter's job to take care of this, that happens at runtime and not at build time.

It does matter for C++ projects because they get compiled to the target architecture at build time. A 64-bit DLL or EXE generated from C++ code is very different from a 32-bit one, it contains very different machine code.

A pure managed solution should therefore have only one Platform selection. Which is "AnyCPU" in old VS versions. And again in new VS versions. Microsoft fumbled it badly at VS2010 when they started creating projects that have "x86" as the default Platform selection. Creating all sorts of misery with solutions that have a drastic mix of platforms when they got upgraded from an old version of VS.

Sounds like you've dug yourself an even deeper hole where the assemblies are getting built to bin\x64\debug but the reference assemblies are still pointing to bin\debug. No real idea how you did this, you must always use project references in a solution with multiple projects that depend on each other.

I recommend serious slash-and-burn to get this problem resolved:

  • Use Build + Configuration Manager to delete extraneous platform selections until you have only one left.
  • Delete reference assemblies in a project's Reference node and re-add them with Project + Add Reference, now using the Project tab.
  • Right-click each project, Properties, Build tab. Every class library project must have its Platform Target setting at AnyCPU. Only the setting on the EXE project matters, that's the one that determines the bitness of the program. Choose between x86 and AnyCPU. Never use x64.
  • Switch to the Release build and repeat the previous step.