Recently I had to deal with this problem: How to start (automatically) a windows service after installing it?
It took me a while to find the answer, so I decided to post this to make it easier for anyone in the same situation.
First, How do I install a windows service? Piece of cake, you have several options: write a script, programatically, using a deployment project. I personally like the last option. This is a step by step guide of how to create setup project for you windows service.
Next step, go to the code view of the Project Installer class and in constructor add a new handler like this:
this.AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall);
Now add this function:
private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
ServiceController controller = new ServiceController("ServiceName");
try
{
controller.Start();
}
catch (Exception ex)
{
String source = "MyService Installer";
String log = "Application";
if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, log);
}
EventLog eLog = new EventLog();
eLog.Source = source;
eLog.WriteEntry(@"The service could not be started. Please start the service manually. Error: " + ex.Message, EventLogEntryType.Error);
}
}
And that's it. Check the Event Viewer and if you get an error check the service name parameter when creating the ServiceController.
Don't forget to add these Namespaces: System.ServiceProcess; System.Diagnostics;
I won't take credit for this, this code is part of a set of tutorials related to windows services I found in Grinn Blog. Thanks Grinn!
If you decide to use a script anyway (instead of doing this programatically) you just need to insert this command (net start [service]) after the installing instruction.

No hay comentarios:
Publicar un comentario