Wednesday, January 30, 2013

Windows Service: Starting a Service on Installation

I have been working on a Window Service project and I need to start the service after it gets installed since I need to manually run it after installation. This is very frustrating for me since I have been testing the service also.

So I have found the following solution which I am going to share with you which solved my problem and Service gets started after installation.

Below is the code for the Installer class which I have used in my Service project:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;


namespace Service
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
            this.AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall);
        }

        void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            ServiceController sc = new ServiceController(serviceInstaller1.ServiceName);
            sc.Start();
        }

        

    }
}

Hope this will help to resolve such issue.


No comments:

Post a Comment