I have developed a windows service, but need it to start automatically on install. Problem is that every tutorial I found is showing me through the Setup Project
. There is a fantastic 3 part tutorial HERE that I used to convert my app to a service, but I don't have the Setup Project
in my other project types. Can I do this programatically or is there a way I can get the Setup Project
project type?
Start windows service on install without setup project
1.4k views Asked by Volearix AtThere are 4 answers
If you are targeting Windows 7 and up, Powershell is installed by default. One option is to run a simple powershell script which installs the service, starts it, and sets the service to start automatically if the machine is rebooted:
InstallUtil yourService.exe
Start-Service yourService
Set-Service yourService -startuptype "Automatic"
Best thing to do is to add on the Installer Projects Extension!
The Setup project type was deprecated after VS 2010, but following feedback Microsoft have brought back a new version for VS 2013.
Install the new Installer Projects Extension from Microsoft: https://visualstudiogallery.msdn.microsoft.com/9abe329c-9bba-44a1-be59-0fbf6151054d
This should work for any non-express version of Visual Studio 2013 (including the new free Community Edition SDK)
Then you can just follow the same instructions as for the VS 2010 setup projects :)
There isn't an easy option for VS2012 (you could try an installer with WiX I guess, but thats a lot to learn!) I'm not sure if InstallShield LE (free version for VS2012) would work for this situation, but you could give it a try.
You can always change the startup type of a service after installation remember. (Control Panel -> Administrative Tools -> Services -> right click service -> Properties -> change "Startup Type" to "Automatic" )
I believe Topshelf project has built-in service Installer/Uninstaller. when integrated in the application, it can be installed as a service through simple command from application itself, for example we can easily install and start myService.exe
with myService.exe install start
command.
we can simply create a self installing service, here is an Example:
public class ServiceClass
{
public ServiceClass()
{
}
public void Start() { }
public void Stop() { }
}
public class Program
{
public static void Main(string[] args)
{
//we can simply install our service by setting specific commands for same or install it directly from command line or from another process
if (args.Length == 0)
{
var processName = Process.GetCurrentProcess().ProcessName + ".exe";
var install = Process.Start(processName, "install start");
install.WaitForExit();
return;
}
HostFactory.Run(x =>
{
x.Service<ServiceClass>(s =>
{
s.ConstructUsing(name => new ServiceClass());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("Topshelf Host");
x.SetDisplayName("TopShelf");
x.SetServiceName("TopShelf");
});
}
}
you can get Topshelf through PM> Install-Package Topshelf
Nuget command.
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.
MSDN link for more details