14 June 2007

How to: Install and Uninstall Services

This program says you have to use the command line program installutil on the executable in order to get a Windows Service installed on the machine. The link above also discusses how to uninstall .Net services if desired.

13 June 2007

Writing Your Own Windows Service in Visual Studio

This entry is indebted to the following articles: MSDN: Walkthrough: Creating a Windows Service Application in the Component Designer and 15 Seconds: Creating an Extensible Windows Service.

The method for writing a Windows Service in Visual Studio is neither straightforward or easy. The main issue is that Windows does not allow Windows Services to display any type of UI or debugging at all. You are required to write your own logging routines in order to get feedback on where and how the application is performing. Following is my first stab at outlining the steps for successfully writing and deploying Windows Services.

Please note, Windows Services can only be written in Visual Studio Pro (or better). The 15 Seconds article talks about manually writing an installer, but the code does not compile. That's ok with me...VS Pro is available, why not use it?

The MSDN provides all the steps needed to write and deploy your service, although it is a little light on the details of why you need to take certain steps. There is something in me that doesn't like to paint-by-numbers. I always like to know the 'why', but I have to start today with the 'How'.

The steps are as follows:

1. Write and debug your program. Make sure you have a logging mechanism and your try/catch blocks are very tight and catch all potential errors and/or provide meaningful logs.

2. Per the MSDN article, add the service to your project. Add a timer object to the project and register your method with the Timer.Elapsed event. An excellent outline of how to write this code is found at the end of the 15 Seconds article above. Pay attention to how to start and stop the timer events, unless you are writing a multi-threaded application.

3. Create a new Setup project. Add your improved program with service to this new Setup project. Set up the file system, packaged files and project as you would like it to be deployed.

4. Right-click on the Setup project node. Select Build. An MSI file (with wizard) will be sent to the debug folder of the Setup project. This MSI file provides a wizard to install your service and create all the files necessary to run the application.