RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Write Your Own Windows Services : Page 4

If you're using Windows, you're no stranger to Windows services, but do you know how to write one of your own? In this article, you'll learn not only how to build your own Windows service, but how to build a client with which to control it.

Installing the Windows Service
It is now time to install the Windows service. Unfortunately, Windows services cannot be executed simply by pressing the F5 function key; you need to do some work.

First, you need to build the project. Right-click on the project name in Solution Explorer and then select Build.

To install the Windows Service, use the InstallUtil utility provided by the .NET Framework. Locate the bin directory containing the executable of your Windows service and type the following:

installutil WordFilesMonitor.exe
For example, if your Windows service project is located in C:\, you should see something like this:

C:\WordFilesMonitor\bin\Debug>installutil WordFilesMonitor.exe

Author's Note: If you cannot access the InstallUtil utility, it is likely your path is not set correctly. The easiest way would be to use the Visual Studio 2005 Command Prompt. This is located at Start | Programs | Microsoft Visual Studio 2005 | Visual Studio Tools. The Visual Studio 2005 Command Prompt has all the relevant path settings to access the tools that come with the .NET Framework.

You can now check if the Windows Service is installed in your system by launching the Services utility (Start | Programs | Administrator Tools | Services) (see Figure 4):

Figure 4. Installation: Verifying if the Windows service is installed successfully.

Recall that in the OnStart event handler, it requires additional information to be passed in (the single input parameter string array), hence getting it started directly from the Services utility is a little tricky. Instead, you will write a Windows application to start it.

Uninstalling the Windows Service
Just as it is important to know how to install a Windows service, you also need to know how to uninstall a Windows service. This is because once the Windows service has started running, you cannot simply modify your Windows service project and overwrite the executable by recompiling the source. You need to stop the service and uninstall it first. To uninstall the Windows service, use the /u option as you would install the service, like this:

C:\WordFilesMonitor\bin\Debug>installutil WordFilesMonitor.exe /u
You can now modify your source codes and do a recompilation. Finally, install the service again.

There are cases in which, after doing the un-installation and recompilation, the new Windows Service may refuse to install. If this should happen, ensure that your Services utility is closed and restarted. When in doubt, the installation message that is displayed by the InstallUtil utility provides some hints as to the cause of the error.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date