Professional Visual Basic 2012 and .NET 4.5 Programming
by Bill Sheldon, Billy Hollis, Rob Windsor, David McCarter, Gastón Hillar, Todd Herman
Debugging the Service
Debugging a service is not as straightforward as debugging other application types, because a service must be run from within the context of the Service Control Manager rather than from within Visual Studio. To debug a service, you must start the service and then attach a debugger to the process in which it is running. You can then debug the application using all of the standard debugging functionality of Visual Studio.
To avoid going through this extra effort, you may want to test most of the code in your service in a test application. This test-bed application can have the same components (FileSystemWatchers, EventLogs, Timers, and so on) as the Windows Service, and thus be able to run the same logic in events. Once you have checked out the logic in this context, you can just copy and paste it into a Windows Service application.
However, sometimes the service itself needs to be debugged directly, so it is important to understand how to attach to the service's process and do direct debugging. You can only debug a service when it is running. When you attach the debugger to the service, you are interrupting it. The service is suspended for a short period while you attach to it. It is also interrupted when you place breakpoints and step through your code.
Attaching to the service's process enables you to ...