4

I have a Visual Studio Setup Project used to install a Windows service. The service is set to run automatically and it is installing fine at this point by adding my service as a custom action. However, I would like the installer to start the service once the installation is complete. Is this possible?

On a related note, do I need to manually stop the service on uninstalls and/or upgrades?

Dennis
  • 532
  • 7
  • 21

2 Answers2

2

Override below custom action handler to start windows service after the installation. Though you set service property type=Automatic, the service won't start unless we explicitly start:

protected override void OnAfterInstall(IDictionary savedState)
{
        using (var sc = new ServiceController(serviceInstaller1.ServiceName))
        {
            sc.Start();
        }
}

Visual Studio installer is not stopping FileInUse prompt though we stop the service using custom action for uninstall/upgrade. Seems like VS Installer doesn't have much sophistication to change the sequence of installer because of that we couldn't control this behavior. But, it is better idea to stop the service for uninstall/upgrades.

RJN
  • 696
  • 8
  • 17
  • I had to put this in the Committed event instead, seems like my service is not available OnAfterInstall yet. – Etherman May 24 '20 at 15:11
1

You can use a custom action too, for starting/stopping the service. From your custom action you just need to call the following utility: http://technet.microsoft.com/en-us/library/cc736564(v=ws.10).aspx

Yes, for upgrades/uninstalls you should stop the service before the files are removed, so you avoid a "Files in Use" prompt from the OS.

Bogdan Mitrache
  • 10,536
  • 19
  • 34
  • For the custom action, I just right-clicked on the "Custom Actions" root element in the setup project and selected "Add Custom Actions..." and chose my service and the installer automatically handled adding the proper custom actions and installing/uninstalling the service itself. It sounds like I need to add some code to start the service itself? Also, I've not had any issues with "file in use" errors with the service running and uninstalling it up to now so I wonder if that is really needed? – Dennis May 10 '12 at 21:09
  • 2
    OK, I think I figured it out. You have to override the Commit method in your service installer class. Then just call new ServiceControl("servicename").Start(). – Dennis May 11 '12 at 15:38