Q: I recently created a Windows service by following the steps in your MSDN article (http://msdn.microsoft.com/msdnmag/issues/01/12/NETServ/). The service works fine and does exactly what I want, but I still have one problem. In the article, you didn't discuss how to install the service on a client computer. Do I have to go around and run INSTALLUTIL.EXE on each computer? That seems like an unreasonable alternative, but it's the only one I see. Just to recap: I created the service, added an installer, set the User property for the ServiceProcessInstaller1 component to LocalSystem, and ran and tested the service locally. Now what? I tried adding a Setup project to my solution, and even installed it, but the service was never added to the list of services. What am I missing?
-- Baghzaad Bhomisha
A: Amazingly, it appears that MSDN article I wrote years ago is still the article people run to when they need to build a Windows service in .NET. It must turn up first in search engines or something. I get lots of e-mail because of it. In any case, that article only showed how to create the service, not how to install it. Because creating Setup projects is such a huge topic, it made sense (at the time, anyway) to leave that for another article. Unfortunately, the next article was never written, so people like Baghzaad are able to build the service, but not able to install it.
In the simplest scenario, installing the service is easy. It just involves adding a Setup project to your solution, and setting one extra property. But let's back up a bit. Every Windows service must run as if it was a session started by a specific user. Windows supplies a number of built-in users, and you can run the service as one of those users, or you can run the service as a particular user in your network. Best practices for network security suggest you create a new user with just enough rights to run the service, and run the service as that user. I'll leave that discussion for another time (you've heard that before, right?), but it's important that you've selected an appropriate user when you create the installer for your service (see the article mentioned previously for more details.) If you don't specify the user at design time, when you attempt to install the service, you'll be prompted with a dialog asking which user the service should run as.
To install your Windows service, you must first add a Setup project. With your service project loaded in Visual Studio .NET, select File > Add Project > New Project. From the list of project types, select Setup and Deployment Projects. From the list of templates, select Setup wizard. Set the location appropriate for your situation and click on OK to create the project. In the Setup wizard, skip page 1. On page 2 (of 5) select "Create a Setup for a Windows application." On page 3, select "Primary output from <yourservice>." On page 4, click on Finish. The final step (a crucial one) is to add a custom action to run the code in the installer class you added to your project (as part of reading the MSDN article). To do that, select View > Editor > Custom Actions. Right-click on the Install node in the Custom Actions treeview. In the Select Item in Project dialog, make sure Application Folder is selected, and click on OK. Click on OK again to select the "Primary output from <yourservice>" option. You should now see this as a child node of the Install node. That's all you have to do. Rebuild both the project and the setup project, run the installation, and the service should be installed as a Windows service.
Of course, this scenario doesn't deal with getting the service started. Even if you've set the ServiceInstaller component's StartType property to Automatic, you have to reboot the computer to start the service. (Or, you can use the services.msc applet and start it yourself.)
As you can see, in the simplest case, adding a setup project that can install a Windows service requires only a single extra step. Not bad for what could be a difficult job.
-- Ken
This Advisor Answer was originally published in the 2005 Week 28 issue of VB.NET ADVISOR. Subscribers can get more Advisor Answers, Advisor Tips, and how-to advice on succeeding with Microsoft .NET technology at http://VBNetAdvisor.com.