The best way to start building a service is to create an assembly with the functionality you want and a test client, before the service is actually created. Here, you can do normal debugging and error handling. As soon as the application is running, you can build a service by using this assembly. Of course, there might still be problems with the service:
¤ Don’t display errors in a message box from the service (except for interactive services that are running on the client system). Instead, use the event logging service to write errors to the event log. Of course, in the client application that uses the service, you can display a message box to inform the user .about errors
¤ The service cannot be started from within a debugger, but a debugger can be attached to the running service process. Open the solution with the source code of the service and set breakpoints. From the Visual Studio Debug menu, select Processes and attach the running process of the service.
¤ The Performance Monitor can be used to monitor the activity of services. You can add your own performance objects to the service. This can add some useful information for debugging. For example, with the Quote service, you could set up an object to give the total number of quotes returned, the time it takes to initialize, and so on.
When an interactive service runs with a logged-on user, it can be helpful to display message boxes to the user. If the service should run on a server that is locked inside a computer room, the service should never display a message box, When you open <Imessage box to wait for some user input, the user input probably won’t happen for some days because nobody is looking <Itthe server in the computer room. Even worse, if the service isn’t configured as an interactive service, the message box opens up on a different, hidden, window station. In this case, no one can respond to that message box because it is hidden, ‘and the service is blocked.
In cases when you really want to interact with the user, an interactive service can be configured. Some examples of such interactive services are the Print Spooler, which displays paper-out messages to the user, and the NetMeeting Remote Desktop Sharing service.
To configure an interactive service, you must set the option” Allow service to interact with desktop” in
the Services configuration tool (see Figure 23-21). This changes the type of the service by adding the
SERVICE_I~ERACTlVE_PROCESS flag to the type.
Services can report errors and other information by adding events to the event log. A service class derived from ServiceBase automatically logs events when the AutoLog property is set to true. The ServiceBase class checks this property and writes a log entry at start, stop, pause, and continue requests.
Figure 23-22 shows an example of a log entry from a service.
You CDnread mort about eoen: logging and how to write custom romts in Chaptn’ 18, “7racing and Events.”