Thursday, September 19, 2013

Service debugging in Windows 8

Prior to Windows 8 you could debug your services by using statements like:
Debugger.Launch();
Now this is being simply ignored. What is reason of that?

Debugger.Launch() would call application with visual user interface. In Windows 8 this is impossible because services can't interact with desktop and run in interactive mode.

To debug service code we added the following code to OnStart method:
while (!Debugger.IsAttached) // waiting until debugger is attached
{
    RequestAdditionalTime(1000);  // prevents the service from timeout
    Thread.Sleep(1000);  // gives you time to attach the debugger
}

Wednesday, September 18, 2013

AnyCPU in Visual Studio 2012

The default setting for .NET projects in Visual Studio 2012 is AnyCPU. But this AnyCPU differs from you've probably seen earlier - in previous versions of Visual Studio. If you run your application on 64-bit machine you will see it runs as 32-bit process!

Actually the default configuration is not AnyCPU but AnyCPU 32-bit preferred. With this configuration IL is compiled to x86 machine code on both 32-bit and 64-bit Windows machines. If you want to change this to old-style AnyCPU, there is a checkbox "Prefer 32-bit" in Project Settings which you may uncheck.

AnyCPU 32-bit preferred is the same as x86 now - the only difference it can be launched successfully on ARM Windows.

More details can be found here:

http://blogs.microsoft.co.il/blogs/sasha/archive/2012/04/04/what-anycpu-really-means-as-of-net-4-5-and-visual-studio-11.aspx