Stacker
Stacker

Reputation: 8237

Windows Service AutoStart

consider a windows service with a setup project , now how can i force the windows service to start after it finish the Installation?

i tried to add project installer and in the commited even i started the service but that would only work if i used InstallUtil im looking for a way to make it while using the Setup Project...

any idea ?

Upvotes: 1

Views: 803

Answers (3)

Tiger Galo
Tiger Galo

Reputation: 351

In order for your service to start right after installation you can add the following few code lines. By subscribing to the Committed event you can make sure it starts the service after being installed. Also pay attention to the service.StartType = ServiceStartMode.Automatic; line, which in its turn takes care of the service to be installed with automatic startup property ON, which makes the service to auto start with system reboot.

public class ProjectInstaller : Installer
{
    private ServiceProcessInstaller process;
    private ServiceInstaller service;

    public ProjectInstaller()
    {
        process = new ServiceProcessInstaller();
        process.Account = ServiceAccount.LocalSystem;
        service = new ServiceInstaller();
        service.ServiceName = "MyWCFServer";
        service.StartType = ServiceStartMode.Automatic;
        Installers.Add(process);
        Installers.Add(service);

        service.Committed += new InstallEventHandler(serviceInstaller_Committed);
    }

    void serviceInstaller_Committed(object sender, InstallEventArgs e)
    {
        ServiceController controller = new ServiceController(service.ServiceName);
        controller.Start();
        controller.WaitForStatus(ServiceControllerStatus.Running);
    }
}

Upvotes: 1

KhanZeeshan
KhanZeeshan

Reputation: 1410

As Johann Blais suggested; First Add a deployment project & Add Custom Action in Both "Install" tab & "Commit" tab do what you require in these Tab, meaning start the service.

Upvotes: 0

Johann Blais
Johann Blais

Reputation: 9469

You could create a new custom action that would use the ServiceController class to start your newly created service.

Upvotes: 1

Related Questions