My.ADVISOR.com Sign-In
ID
Password

Member Center / Sign-Up
Go to Article
Advanced Search 

ADVISOR ANSWERS

Install a Windows Service

Learn how to build a Windows service in .NET.

By Ken Getz, technical editor

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.

What do YOU think about this topic? Share your advice and thoughts using this form.

Your Name

REQUIRED : PUBLIC

Your E-Mail

REQUIRED : PRIVATE

Job, Company

OPTIONAL : PUBLIC

City, State, Country

OPTIONAL : PUBLIC

Your Web Site

OPTIONAL : PUBLIC

Your Comment

Please help everyone by keeping your comments on-topic, using clean language, and not defaming or making personal attacks.


Your e-mail address is required, but it will not be displayed to the public or given to anyone. See our Privacy Policy. Comments become visible after they pass our spam filter, and spammers and abusers are permanently blocked. Please report spam or abuse.

Ken GetzTechnical Editor Ken Getz is a programmer, technical writer, educator, and senior consultant with MCW Technologies. He's a lead courseware author and presenter, he develops custom Windows and Web applications and tools using Visual Studio and Microsoft Office, and he's been a Microsoft MVP award winner since the program began. Ken is a frequent speaker at Microsoft events, ADVISOR SUMMIT, and other technical conferences. He's co-author of ASP.NET Developer's Jumpstart, Access Developer's Handbook series, and VBA Developer's Handbook. http://www.mcwtech.com/cs/blogs/keng

Printer-friendly
page layout

Keyword Tags: Code, Development, Microsoft, Microsoft Visual Basic, Microsoft Visual Basic .NET, Microsoft .NET, Windows Services

ADVISORAMA
The murals in restaurants are on par with the food in museums.
-- Peter De Vries

ARTICLE INFO

DataBased Advisor

Web Edition: 2006 Week 04, Doc #17602

FREE ACCESS FREE ACCESS

SUBSCRIPTION STATUS
You are not signed-in. If you are a subscriber to this publication, sign-in above to access locked articles. To subscribe or renew go to www.AdvisorStore.com.

Subscribe to DATABASED.ADVISOR.com

Get it all -- every current and past ADVISOR tech/business publication, now all-in-one subscription, with new articles and a huge reference library packed with expert advice, how-to and downloads. Subscribe now to get it all.

DataBased.Advisor.com

Subscribe to FileMaker Advisor Magazine

Read the advanced guide to creating custom business database solutions with FileMaker software. Subscribe now to gain access to all the archives and downloads.

FileMaker.Advisor.com

Subscribe to Advisor Basics of FileMaker Pro

Learn the fundamentals of using FileMaker Pro software. Every issue gives you step-by-step instructions on creating the databases you need. Subscribe now!

FileMaker.AdvisorBasics.com

Showcase Your Smarts

Submit your tips, techniques and advice and let Advisor promote your business and build your career. Show the world what you know!

AdvisorTips.com

Use of this or any other site, content, product or service of Advisor Media constitutes acceptance of Terms of Use.
Portions copyright ©1983-2008 Advisor Media, Inc. All Rights Reserved.
Reuse or reproduction of any portion or quantity of Advisor Media's copyrighted content, in any form, for any purpose, requires written permission.
ADVISOR®, the ADVISOR logo, and other names and logos that incorporate ADVISOR are registered trademarks, trademarks or service marks of Advisor Media, Inc. in the United States and/or other countries.
Other trademarks are used for identification, editorial or descriptive purposes and are the property of their owners.
Hosted by Prominic.NET Website powered by
LOTUS SOFTWARE
GETZK164-01 posted 03/22/2006 modified 01/07/2009 03:42:51 AM ztdbms/ztdbms
domino-144.advisor.com my.advisor.com 01/07/2009 04:44:59 PM