Automatically Start a Windows Service on Install

Automatically start a Windows Service on install

In your Installer class, add a handler for the AfterInstall event. You can then call the ServiceController in the event handler to start the service.

using System.ServiceProcess;
public ServiceInstaller()
{
//... Installer code here
this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}

void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
ServiceInstaller serviceInstaller = (ServiceInstaller)sender;

using (ServiceController sc = new ServiceController(serviceInstaller.ServiceName))
{
sc.Start();
}
}

Now when you run InstallUtil on your installer, it will install and then start up the service automatically.

Automatically start a windows service after install

I consider the latter a little more proper (although a quick check of my codebase and I coded essentially the former). The difference I see is the chance of a Rollback happening. In the commit phase you are past the risk of a rollback. But if you start your service in the AfterInstall (which is just part of the overall Install phase (the four phases being Install, Rollback, Commit, Uninstall)) you have the possibility of a Rollback being issued by a subsequent InstallerClass. You would then need to stop your service and uninstall it (which Microsoft's default service installer classes do for you so it's not much of an issue.

In summary, there isn't too much of a difference.

How to autostart windows service after install by sc?

This is possible by using either the net start service or sc start command (see previous question on that).

To start a service using sc start, the syntax is:

sc [<ServerName>] start <ServiceName> [<ServiceArguments>]

<ServerName>
Specifies the name of the remote server on which the service is located. The name must use the Universal Naming Convention (UNC) format (for example, \\myserver). To run SC.exe locally, omit this parameter.
<ServiceName>
Specifies the service name returned by the getkeyname operation.
<ServiceArguments>
Specifies the service arguments to pass to the service to be started.

Example:

sc start MyService


Updated script:

@echo OFF
echo Installing service...
sc create "MyService" binpath= %~dp0\MyService.exe start= auto
sc start MyService
echo Installing service complete
pause

C# Installing a service and set to automatic start on boot

It seems this question has been answered here already:
How do I change a Windows Service's startup type in .NET (post-install)?

This seems to be the most upvoted answer:

var svc = new ServiceController("ServiceNameGoesHere");  
ServiceHelper.ChangeStartMode(svc, ServiceStartMode.Automatic);

Automatically Start Windows Service on Install

I ended up taking a different approach by just getting the running process instead of trying to hold on to the one started before. Stopping the process then works just fine, and doesn't matter of the OnStart/OnStop methods are functioning "properly", because my inner process is starting/stopping like I need.

Run windows service automatically forever, after starting manually for the first time

Your code already seems to be setting the startup type to Automatic.

Can you check the event log to see if your service is attempting to start automatically, but failing. This can happen if your service depends on another service which has not yet started.

Start windows service on install without setup project

In your Installer class, add a handler for the AfterInstall event. You can then call the ServiceController in the event handler to start the service.

public ServiceInstaller()
{
//... Installer code here
this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}

void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
using (ServiceController sc = new ServiceController(serviceInstaller.ServiceName))
{
sc.Start();
}
}

Now when you run InstallUtil on your installer it will install and then start up the service.

MSDN link for more details

How to autostart windows services in C#

namespace curUsers
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
var processInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();

//set the privileges
processInstaller.Account = ServiceAccount.LocalSystem;

serviceInstaller.DisplayName = "curUsers";
serviceInstaller.StartType = ServiceStartMode.Automatic;

//must be the same as what was set in Program's constructor
serviceInstaller.ServiceName = "curUsers";

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

private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{

}

private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{

}
}
}

Just try this, all of my windows services are developed in the same way. this one also works well.



Related Topics



Leave a reply



Submit