三次尝试后,Topshelf暂停服务恢复

Tro*_*sen 5 c# windows-services topshelf

我正在使用Topshelf创建Windows服务。该服务将尝试恢复前3个故障,但此后将不再起作用。

在主机上的“服务”中检查服务将显示:

First Failure:          Restart the Service
Second Failure:         Restart the Service
Subsequent Failures:    Restart the Service
Reset fail count after: 1 days
Restart service after:  2 minutes
Run Code Online (Sandbox Code Playgroud)

服务恢复代码如下所示:

f.EnableServiceRecovery(r =>
{
    r.RestartService(2);
    r.RestartService(5);
    r.RestartService(5);
    r.OnCrashOnly();
    r.SetResetPeriod(1);
});
Run Code Online (Sandbox Code Playgroud)

恢复失败后,检查事件日志将显示以下消息:

The MyService service terminated unexpectedly.  It has done this 1 time(s).  The following corrective action will be taken in 120000 milliseconds: Restart the service.
The MyService service terminated unexpectedly.  It has done this 2 time(s).  The following corrective action will be taken in 300000 milliseconds: Restart the service.
The MyService service terminated unexpectedly.  It has done this 3 time(s).  The following corrective action will be taken in 300000 milliseconds: Restart the service.
The MyService service terminated unexpectedly.  It has done this 4 time(s).
Run Code Online (Sandbox Code Playgroud)

从上面显而易见。第四次不触发恢复。

这是Windows错误,Topshelf问题还是我的配置有问题?

小智 4

您必须为 topshelf 恢复设置设置底部配置:

\n\n
x.EnableServiceRecovery(rc =>\n                {\n                    // Has no corresponding setting in the Recovery dialogue.\n                    // OnCrashOnly means the service will not restart if the application returns\n                    // a non-zero exit code.  By convention, an exit code of zero means \xe2\x80\x98success\xe2\x80\x99.\n                    rc.OnCrashOnly();\n                    // Corresponds to \xe2\x80\x98First failure: Restart the Service\xe2\x80\x99\n                    // Note: 0 minutes delay means restart immediately\n                    rc.RestartService(delayInMinutes: 0); \n                    // Corresponds to \xe2\x80\x98Second failure: Restart the Service\xe2\x80\x99\n                    // Note: TopShelf will configure a 1 minute delay before this restart, but the\n                    // Recovery dialogue only shows the first restart delay (0 minutes)\n                    rc.RestartService(delayInMinutes: 1); \n                    // Corresponds to \xe2\x80\x98Subsequent failures: Restart the Service\xe2\x80\x99\n                    rc.RestartService(delayInMinutes: 5);\n                    // Corresponds to \xe2\x80\x98Reset fail count after: 1 days\xe2\x80\x99\n                    rc.SetResetPeriod(days: 1); \n                });\n
Run Code Online (Sandbox Code Playgroud)\n\n

看看样本

\n

  • 您的答案中的代码与OP中的代码之间有什么重要/显着的区别? (2认同)