问题
I have a custom C# Windows Service.
It has detailed logging set up and graceful shutdown processes, which include logging out when shutting down.
Specifically, the program is an .exe
, which calls
ServiceBase.Run(container.Resolve<MyWindowsService>());
in its Program.Main()
method.
I have logging in the (overridden) MyWindowsService.OnStop()
method, and in a finally
block of the Program.Main()
method.
The service is installed and configured for Automatic
start in Windows Services.
When running, or stopping "normally" everything is fine, and all that logging works just fine. If I manually stop the service it's all good, and I get my logging. If I restart the machine, then it seems to reliably restart the service on log in.
But if I log out and log back in again, then sometimes it doesn't restart.
I think that I've observed all 3 of: * Log back in, and it's still running, with no "I just started" logs. * Log back in, and it's still running, but the logs say that it restarted. * Log back in, and it's not running.
The 3rd option seems to correlate with Event Logs saying
Event 7034 Source - service control manager.
MyWindowsService
terminated unexpectedly. It has done this X time(s).
How can I ensure that the service is always started?
Separate question asked, for "How do I ensure the logs always get written?", which I suspect will reveal the solution. But to prevent shouts of "this is an X-Y Question". I've asked the direct problem here.
来源:https://stackoverflow.com/questions/60303036/how-to-ensure-that-a-service-is-always-running-after-logging-out-and-back-in-aga