How do you automatically start a service after running an install from a Visual Studio Setup Project?
I just figured this one out and thought I would share the answer for the general good. Answer to follow. I am open to other and better ways of doing this.
Go to start type services. msc and press enter. On the services list that opens up, right click on the service and select Properties. The dialog that opens has an option 'Automatic' for starting your service.
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. Now when you run InstallUtil on your installer, it will install and then start up the service automatically.
Go to Visual C# and select Classic Desktop and from the next window, choose Windows Service. Name the service “MyFirstWindowsService” and click to OK. It will add a Windows Service for you. Rename the Service1.
Add the following class to your project.
using System.ServiceProcess; class ServInstaller : ServiceInstaller { protected override void OnCommitted(System.Collections.IDictionary savedState) { ServiceController sc = new ServiceController("YourServiceNameGoesHere"); sc.Start(); } }
The Setup Project will pick up the class and run your service after the installer finishes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With