One of the most difficult task for a entry to mid-level .NET programmer is debugging a .NET Windows Service while it is live. Even if you do have a development server that you can use, debugging existing code may seem a mysterious task at first. I remember a few years back when I was researching this topic on the web, no one gives you a straight and simple answer about this topic. So here is my attempt at simplifying this topic a bit.
Windows Service Architecture
- Service Application: This type application is basically the actual Windows .NET Service that runs under Services.
- Service Controller Application: This type of application enables you to control the behavior of an existing application, start, stop, pause and continue a service.
- Service Control Manager: This is a utility that comes with Windows that enables you to control all services that are installed on a computer. It can be found at "Control Panel > Administrative Tools > Services".
Windows Service: System.ServiceProcess Installer Namespaces: System.ServiceProcess.ServiceInstaller and ServiceProcessInstaller Service Controller: System.ServiceProcess.ServiceController
The registry location that contains a list of all services running on a Windows Machine is:
Here is how a typical registry entry looks when a .NET Service is installed:
The most notable one is the "ImagePath" entry, which contains the path to the executable that runs the service. This value can be change to point to different versions of the service executable for testing purposes.
Services HAVE TO be installed before running them the executable, in order to use them. You can NOT simply press RUN in Visual Studio and run them that way, or double click on the executable and expect to interact with them! You can NOT debug Windows Services using F5, F10 or F11 stepping through code method. In order to be able to do that, the service has to be installed, started under Windows Service Controller, and its process HAS to be attached to its source code in Visual Studio. Any Message Boxes, Dialog Boxes, or Windows Forms opened or used by a Windows Service are HIDDEN from the user, unless you check the checkbox under Service Properties that says "Interact with Desktop", in which case you will see them all! Error Messages that occur in a Windows Service are logged in the Machine's "Event Log".
The following is a list of Methods that can / have to be overriden in order to run the service successfully or run a Service Controller Application:
- Methods of Service Base Class (to Override)
- Methods of Service Controller Class
Click HERE to go to Part 2 of this article.