trying to get windows service to auto-start after install
-
I've written a Windows Service and I'm trying to get it to auto-start after install. The "Startup Type" is "Automatic" but this only starts the service after a computer restart. I've found a few posts that recommend the following code: public ProjectInstaller() { InitializeComponent(); this.AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall); } void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e) { ServiceController sc = new ServiceController("TestWindowsService"); sc.Start(); } However, this code doesn't appear to be starting the service for me after install. Any suggestions on what might be missing?
-
I've written a Windows Service and I'm trying to get it to auto-start after install. The "Startup Type" is "Automatic" but this only starts the service after a computer restart. I've found a few posts that recommend the following code: public ProjectInstaller() { InitializeComponent(); this.AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall); } void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e) { ServiceController sc = new ServiceController("TestWindowsService"); sc.Start(); } However, this code doesn't appear to be starting the service for me after install. Any suggestions on what might be missing?
I think you need to initiate another event:
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();
}Have a look here: Auto Start Windows Service After Installation[^]