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.


Note
Don't attach to a process unless you know what the process is and understand the consequences of attaching to and possibly killing that process.

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 debug most, but not all, of the service's code. For instance, because the service has already been started, you cannot debug the code in the service's OnStart method this way, or the code in the Main method that is used to load the service. To debug the OnStart event or any of the Visual Studio designer code, you have to add a dummy service and start that service first. In the dummy service, you would create an instance of the service that you want to debug. You can place some code in a Timer object and create the new instance of the object that you want to debug after 30 seconds or so. Allow enough time to attach to the debugger before the new instance is created. Meanwhile, place breakpoints in your startup code to debug those events, if desired.

Aside from issues with an issue occurring at startup to debug a running service, you'll need to ensure you have the appropriate permissions. This means you need to have started Visual Studio with Administrator privileges. Once you have done that you can perform the following steps:

1. Verify the service is installed and running.
2. Load the solution for the service in Visual Studio. Then select Attach to Process from the Debug menu. The Attach to Process dialogue appears (see Figure 16.5). Be sure to enable the check boxes next to the “Show processes from all users.”

Figure 16.5 Attach to a running process dialogue

16.5
3. In the Available Processes section, click the process indicated by the executable name (ProVB_FileWatchter.exe) for the service, and then click Attach.
4. You can now debug your process. Place a breakpoint in the code for the service at the place you want to debug. Cause the code in the service to execute (by placing a file in a monitored directory, for example).
5. When finished, select Stop Debugging from the Debug menu.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset