views:

367

answers:

3

I've built a Windows service as "Any CPU". However, when I run it on my 64 bit machine it runs in 32 bit. How can I fix it? I'm using .NET and C#, and my operating system is Windows 2008 R2.

If I build it in x64 it correctly loads in 64 bit mode. However, "Any Cpu" -- which is what I want -- loads in 32 bit, even though the machine it's running on perfectly supports 64 bit.

Thanks for any help.

EDIT to add more information based on feedback

We do have third party tools as well as reference a c++ managed assembly. These may or may not be built for any CPU. In fact i know that the c++ managed assembly is only built for x86. However, the odd things is that if I specifically specify x64 the process will start up and work in x64. If the framework were to attempt to load the c++ managed assembly it would fail. I don't mind this, because in the code, we do not load the 32bit managed ++ assembly if we're running in 64 bit mode. Could it be that the build figures that since there is a 32 bit assembly in here it should mark the launching process (in this case a windows service assembly) as x86?

+1  A: 

Perhaps the answers to this question will help?

Update: Lots more info from Rick Byers.

Stephen Kellett
A: 

If the app exe which starts the CLR is compiled as:

  1. x64: AnyCPU assemblies with use JIT to compile x64 versions of the assemblies (slow). x86 assemblies will get a BadImageFormatException.
  2. x86: AnyCPU assemblies JIT to x86. x64 assemblies will get a BadImageFormatException.
  3. AnyCPU: .Net will default to x86. See above.

I often use a starter application, where I explicitly compile the .exe as x64 or x86 and ship x86 and x86 versions of the .msi. This is probably the easiest route - The performance gain is generally worth the overhead of managing additional files.

Alternatively, you can use ngen during the setup process, which will do all the JIT stuff when the application is installed. You will notice improved start-up times for your application.

Also, as I recently discovered with x86 applications, there is a 2GB memory limit for .net applications. This occurs even when you have 4gb of ram on your x86 and have heaps of memory left over.

The msdn says something different: http://msdn.microsoft.com/en-us/library/zekwfyz4.aspx"On a 64-bit Windows operating system ... - Executables compiled with the /platform:anycpu will execute on the 64 bit CLR - DLLs compiled with the /platform:anycpu will execute on the same CLR as the process into which it is being loaded"
PetPaulsen