Home > C#

.NET Windows Service Installer and Auto Start After Installation

6. November 2009

It’s not very often that I need to build a Windows Service for a specific task, and it’s even more rare that I need to create an Installer (.msi) to install/uninstall the service. Well, the project I’m currently working on (http://virtualrouter.codeplex.com) requires me to 1) Install a Windows Service using a Setup Project, and 2) Start the Windows Service immediately after installation.

Create Setup Project for Windows Service

Just create a Setup Project within your Solution in Visual Studio, then follow the below steps:

1) Right-Click the Setup Project you just created
2) Click “Add”, then “Project Output…”
3) In the dialog that appears select your Windows Service Project as Primary Output, then click OK.

That’s really all that’s required. When the resulting Installer is executed it will Install / Uninstall the Windows Service.

You can find a longer description of how to do this here:

http://support.microsoft.com/kb/317421

Auto Start Windows Service After Installation

Whether you use a Setup Project of the “installutil” tool to install your Windows Service, it can be very convenient if the service were to automatically start once installed. To do this all it takes is adding a couple lines of code to the ServiceInstaller that you have defined within your Windows Service Project to handle the “Committed” event, then use the ServiceController class to Start the service.

Here’s an example ServiceInstaller with the “Auto Start” code in place:

[RunInstaller(true)]
public class ServiceInstaller : Installer
{
    string strServiceName = "MyServiceName";

    public ServiceInstaller()
    {
        var processInstaller = new ServiceProcessInstaller();
        var serviceInstaller = new ServiceInstaller();

        processInstaller.Account = ServiceAccount.LocalSystem;
        processInstaller.Username = null;
        processInstaller.Password = null;

        serviceInstaller.DisplayName = strServiceName;
        serviceInstaller.StartType = ServiceStartMode.Automatic;

        serviceInstaller.ServiceName = strServiceName;

        this.Installers.Add(processInstaller);
        this.Installers.Add(serviceInstaller);

        this.Committed += new InstallEventHandler(ServiceInstaller_Committed);
    }

    void ServiceInstaller_Committed(object sender, InstallEventArgs e)
    {
        // Auto Start the Service Once Installation is Finished.
        var controller = new ServiceController(strServiceName);
        controller.Start();
    }
}

 

Conclusion

These are two tips that for some reason had eluded me for a long time. Usually I just created a .bat file to call “installutil” and “net start” to install and start my services manually. In most cases that worked perfect since it was very rare that the service was installed on a new computer or updated to a newer version. However, while building an “End User Friendly” Installer that anyone can run, you need to automate the installation and start up of the Windows Service.

This is just the thing that I’m running into with the new http://virtualrouter.codeplex.com project that I’m working on. The “Virtual Router” project utilizes Windows 7’s Virtual Wifi and Wireless Hosted Network API’s to turn any computer into a Wireless Access Point / Router. Look for the first release of this project soon!

C# , , ,



Comments

11/11/2009 2:29:20 AM #
I've recently started to use TopShelf (http://code.google.com/p/topshelf/) in my Windows Services, as it helps a lot to automate some of the boilerplate code associated with services. Would that have helped in your problem? All you would need to do with a TopShelf based service is run the process with a couple of command line parameters, and it would both install the service and start itself.
Doug Goulden
Doug Goulden
11/12/2009 4:21:09 AM #
Nice tip, thank you very much. This will definitely save me a lot of hassle trying to explain to someone how to either start a service manually or restart the computer.
11/15/2009 10:27:29 AM #
If you are fine without setting any security related properties to your service you go fine without all the nasty PInvoke stuff Wink
11/29/2009 12:46:43 PM #
Pretty Interesting post. Couldnt be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this post to him. Pretty sure he will have a good read. Thanks for sharing!
11/29/2009 1:03:45 PM #
Pretty Interesting post. Couldnt be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this post to him. Pretty sure he will have a good read. Thanks for sharing!
11/30/2009 2:51:56 AM #
I will try for Auto Start Windows Service After Installation.Thanks for the given code.
12/1/2009 3:43:06 AM #
I have successfully installed it.
12/2/2009 10:28:59 PM #
Thanks for sharing with this info with us.Its really very helpful for me.
12/4/2009 6:11:10 PM #
Well I think it is good to create your own setup project for window installation and moreover it will immediately start the window service. I am a .net developer. It will help me a lot.
12/7/2009 6:05:45 PM #
I had a great time reading around your post as I read it extensively. I am looking forward to hearing more from you.
12/10/2009 9:31:01 PM #
thanks!  very helpful post!! like the template btw ;)
12/11/2009 7:34:25 PM #
Hello, It seem your blog doesnt display decently with ie6.
12/11/2009 8:04:44 PM #
Good Day, It seem this url does not display decently inside internet explorer6 .
12/11/2009 8:53:36 PM #
Hello, It im sure website doesnt viewed justly inside google chrome.
12/11/2009 9:36:01 PM #
Hi There, It im sure this site does not display fittingly in ie6.
12/11/2009 11:50:29 PM #
Hi There, It im sure this site doesnt viewed correctly in ie6.
12/11/2009 11:57:54 PM #
Hi There, It im sure this site doesnt viewed correctly in ie6.
12/16/2009 3:11:56 PM #
Pretty intresting post.
[url=www.goarticles.com/cgi-bin/showa.cgi?C=1829378]Acne[/url]      
      
12/19/2009 10:58:55 AM #
These steps really help those who don't have any information about this.
But I think these are little bit confusing also.

12/19/2009 1:41:21 PM #
I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!
12/20/2009 12:23:00 PM #
Thanks a lot for sharing Smile
12/20/2009 8:09:34 PM #
Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often. Thumbs up!
12/21/2009 12:43:22 AM #
Very creative, one of the nicer sites I have seen today. Keep up the great work.
12/21/2009 3:09:22 AM #
This is my first time i visit article. I collected so many interesting things in your blog especially its discussion. From the tons of comments on your articles, I think I am not the only one having all the fun here! keep up the good work.Regards
12/24/2009 6:29:28 PM #
Me and my friend were arguing about an issue similar to this! Now I know that I was right. lol! Thanks for the information you post.
phong
phong
12/25/2009 11:08:04 PM #
Thank you very much!
12/25/2009 11:13:31 PM #
Thanks for the information
12/26/2009 9:27:21 PM #
Thank you for the work you have put into your nice blog. We will bookmark to your blog because it is very informational. We love the site and will come back to see your new posts.
12/27/2009 11:28:11 PM #
your post give me useful information about .Net window service
thanks keep posting
12/29/2009 3:18:56 AM #
Yea nice Work !Laughing
12/31/2009 3:05:20 PM #
This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article

12/31/2009 5:02:11 PM #
טיסות לניו יורק
1/11/2010 11:55:31 PM #
Thank you for the sensible critique. Me & my neighbour were preparing to do some research about that. We got a good book on that matter from our local library and most books where not as influensive as your information. I am very glad to see such information which I was searching for a long time.This made very glad Smile
1/12/2010 12:34:30 PM #
watch naruto episodes online at http://narutoepisodes.us/naruto-episodes
1/14/2010 3:03:28 PM #
That is what I call a fantastic post.Thanks alot for sharing.
1/14/2010 10:11:59 PM #
While this subject can be very touchy for most people, my opinion is that there has to be a middle or common ground that we all can find. I do appreciate that youve added relevant and intelligent commentary here though. Thank you!
1/16/2010 6:36:31 PM #
Aw, this was a really quality post. In theory I'd like to write like this too - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done.
1/19/2010 8:25:51 AM #
it was fine with me from the start to the end
Mark
Mark
1/22/2010 4:45:37 AM #
Very good and detailed post there OP. Apprecaiate the time you put into this.

Regards
www.prlog.org/...ancy-treating-pregnancy-acne.html
www.prlog.org/...roducts-devices-and-remedies.html
1/22/2010 6:02:38 AM #

Nice 1 indeed !!!
1/22/2010 7:03:15 AM #
Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often. Thumbs up!
1/23/2010 12:36:06 AM #
Useful info. Hope to see more good posts in the future.
1/23/2010 11:22:53 AM #
This is what I was looking for, quality content, thanks for sharing.
1/24/2010 6:27:51 AM #
crazy huh ... and you haven't seen it all
1/27/2010 5:06:42 PM #
Your code helped me a lot. I used your coding in my project.
1/28/2010 5:32:20 AM #
You can use the fanciest computers to gather the numbers, but in the end you have to set a timetable and act.
1/29/2010 6:03:20 PM #
Don’t stop writing, you’ve given me lots of good info!
1/29/2010 9:33:56 PM #
Thanks heaps to the author!
2/1/2010 2:52:39 PM #
Hi,

How long have you been in this field? You seem to know a lot more than I do, I’d love to know your sources!


2/1/2010 5:19:40 PM #
Do you accept guest posts? I would love to write couple articles here.
I was wondering what is up with that weird gravatar??? I know 5am is early and I'm not looking my best at that hour, but I hope I don't look like this! I might however make that face if I'm asked to do 100 pushups. lol
SEO
2/2/2010 5:06:12 AM #
Hi,

Very useful info. Hope to see more posts soon!

2/2/2010 5:44:54 AM #
Hi,


Top post. I look forward to reading more. Cheers
2/2/2010 6:17:17 AM #
Excellent blog post, I look forward to reading more.
2/2/2010 7:12:45 AM #
It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks
2/3/2010 2:58:10 PM #
Its a good software but Microsoft always like to give thinks that are crap to people and the way they make lots of money with operating systems.

Anyway thats how I feel about them.
Comments are closed