How to ensure that a service is always running after logging out and back in again

白昼怎懂夜的黑 提交于 2020-02-23 07:06:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!