我们有一个用C#编写的Windows,它基本上是在某个端口上启动一个Web。服务被配置为在第一次故障和第二次故障时重新启动。“后续失败”设置为“不采取任何行动”。如果这个端口可能被占用,服务会以未处理的异常崩溃,而在未处理的异常回调中,我们会将转储文件写入某个应用程序目录。无论出于什么原因,Windows总是一次又一次地重新启动该服务,即使它已经多次崩溃。我们的服务结构如下:
public class WinService : ServiceBase
{
private WebApiHostWrapper _apiHost;
private Thread _workerThread;
public WinService()
{
InitializeComponent();
ServiceName = "MyService";
// register handler for writing dumpfiles
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptions.DomainUnhandledException;
}
protected override void OnStart(string[] args)
{
_workerThread = new Thread(InternalStart) { Name = "StartupThread" };
_workerThread.Start(args);
}
private void InternalStart(object args)
{
if (null == _service)
{
Thread.MemoryBarrier();
_apiHost= new WebApiHostWrapper();
_apiHost.Start((string[])args); // exception here
}
}
protected override void OnStop()
{
if (null != _workerThread)
{
_apiHost.Dispose();
_apiHost= null;
if (!_workerThread.Join(5000))
{
_workerThread.Abort();
}
Thread.MemoryBarrier();
_workerThread = null;
}
}在Windows事件日志中,我看到4个条目。
在端口已经在使用的情况下,这会导致服务一次又一次崩溃,使系统充斥转储文件。Windows将始终独立于设置重新启动服务。是否有一种特殊的方法,如何使“后续故障”得到考虑,而不重新启动服务?
发布于 2016-08-15 07:22:26
我发现了导致“第一次失败”被执行的问题。我们将“重置失败计数”设置为0天。将此值设置为1后,服务崩溃2次,然后不再重新启动。
资料来源:
Clarification on Windows Service Recovery Actions Settings
http://www.happysysadm.com/2011/12/understanding-windows-services-recovery.html
https://stackoverflow.com/questions/38919957
复制相似问题