Restart Windows Service when faulted

2020-08-02 10:07发布

问题:

We have an windows service written in .net, that is hosting WCF service. My question is, Is it possible to monitor and restart service on any fault, in other world I want my service to be restarted automaticaly on any fault.
Thanks a lot

回答1:

Start / Control Panel / Services / right-click your service / Properties / First failure : restart the service / Second failure : restart the service / Subsequent failure : restart the service.

Should do it.



回答2:

Go into the service manager in the OS, right-click your service and choose properties. There you'll find a recovery tab which lets you define behavior on crashes.



回答3:

Within single windows service there can be multiple WCF services. You can handle Faulted event of the ServiceHost class for individual WCF service and re-initialize it.



回答4:

I think you should think about the design of your service. It should be a host to the wcf process. You could make 3 threads:

  • Main thread (only stops on v FATAL error)

  • ExceptionHandler thread (handles "expected" exceptions and does logging and takes care of the WCFHost thread dying)

  • WCFHost thread (Hosts the WCF Service and can die as often as it likes the ExceptionHandler can take care of this now.)



回答5:

You could write another service like a watcher service that will monitor this core service & will re-start it when stopped. However, nothing can be done if the watcher service is stopped.