Start a Windows Service Automatically after Install

Recently i came across a situation wherein i had to host a WCF service as a Windows Service.Only after i installed the service using InstallUtil –i  “assembly name”,did i realize my folly that i had a misconception regarding the term “Automatic”.What i am referring to the ServiceInstaller.StartType property.The main intent of this property is to start the Service automatically on start of Windows.

Code Snippet
  1. this.serviceInstaller1.StartType =
  2.      System.ServiceProcess.ServiceStartMode.Automatic;


So what do i do in run the service while installation?All you need to do add the event AfterInstall for the ServiceInstaller and then call the ServiceController from the eventhandler to start the service.

Code Snippet
  1. [RunInstaller(true)]
  2.     public partial class ServiceInstaller : Installer
  3.     {
  4.         public ServiceInstaller()
  5.         {
  6.             InitializeComponent();
  7.             ServiceProcessInstaller processInstaller = new ServiceProcessInstaller();
  8.             ServiceInstaller serviceInstaller = new ServiceInstaller();
  9.             //Specifies the type of account that should be used for the service.
  10.             processInstaller.Account = ServiceAccount.LocalSystem;
  11.             //Specifies the name of the service that is installed.
  12.             serviceInstaller.DisplayName = "Josh Service";
  13.             //Gives the description of the Installed service.
  14.             serviceInstaller.Description = "this is a Win Service";
  15.             //It tells which is the service that should be installed.
  16.             serviceInstaller.ServiceName = "MyService";
  17.             //It specifies the Start type of the service.
  18.             serviceInstaller.StartType = ServiceStartMode.Automatic;
  19.             this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
  20.             Installers.Add(processInstaller);
  21.             Installers.Add(serviceInstaller);
  22.         }
  24.         void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
  25.         {
  26.             ServiceController sc = new ServiceController("MyService");
  27.             sc.Start();
  28.         }
  29.     }



The  service on local computer started and then stopped. Some Services stop automatically if they are not in use by another services or programs.”

You are all used to debugging and would have most likely than not used the “attach to process ” to debug your service implementation.But what do you do when you get Errors or have to debug the Windows Service installer itself?Bingo, we can always write “System.Diagnostics.Debugger.Launch();” in the OnStart() method and start debugging.Also you can write the errors to the EventLog or dump the Exception to a Log after putting in place a simple try catch!

4 thoughts on “Start a Windows Service Automatically after Install

  1. You really make it seem so easy with your presentation
    but I find this matter to be actually something which I think I would never understand.
    It seems too complex and extremely broad for
    me. I am looking forward for your next post, I’ll try to get the hang of it!

  2. Please let me know if you’re looking for a article writer for your site. You have some really great articles and I think I would be a good asset. If you ever want to take some of the load off, I’d really like to write some articles for your blog in exchange for a link back to mine.
    Please shoot me an email if interested. Kudos!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s