0

Is there any way to restart a windows service from the same service , as Application.Restart() in Windows forms, I don't want to launch another process from the service to restart the service.

bobbymcr
  • 23,769
  • 3
  • 56
  • 67
Priyan R
  • 1,042
  • 1
  • 15
  • 24
  • 1
    Already answered here: http://stackoverflow.com/questions/220382/how-can-a-windows-service-programmatically-restart-itself – bobbymcr Dec 06 '09 at 17:32

2 Answers2

1

I am a developer for an open source windows service hosting framework called Daemoniq. Setting service recovery options is one of its features. You can download it from http://daemoniq.org

Current features include:

  • container agnostic service location via the CommonServiceLocator
  • set common service properties like serviceName, displayName, description and serviceStartMode via app.config
  • run multiple windows services on the same process
  • set recovery options via app.config
  • set services depended on via app.config
  • set service process credentials via command-line
  • install, uninstall, debug services via command-line

Thanks!

jake.stateresa
  • 226
  • 2
  • 3
0

You also can add Custom Action to Commit folder of Custom Actions in your setup project. It must be a primary output of class library project with class inherited from System.Configuration.Install.Installer with [RunInstaller(true)] attribute. In this class you need to override one base method:

    public override void Commit(IDictionary savedState)
    {
        base.Commit(savedState);
        ProcessStartInfo psi = new ProcessStartInfo("sc", "failure \"You service name\" reset= 60 actions= restart/1000");
        psi.CreateNoWindow = true;
        Process proc = Process.Start(psi);
        proc.WaitForExit();
    }

It's configuring your service to restart automaticaly after failure.

Than when you need to restart your service you can do

Environment.FailFast("Self restarting service...");

But it has one drawback - it will be fired an error message in event log.

Vladimir Shiyanov
  • 1,236
  • 16
  • 18