间歇性错误 - 服务未及时响应启动或控制请求

Intermittent Error - The service did not respond to the start or control request in a timely fashion

我有一个 windows 服务,我正在使用章鱼部署它。部署在 90% 的时间内运行良好。如果我在清晨部署服务,我将收到 "The service did not respond to the start or control request in a timely fashion." 错误。如果我 运行 从 Octopus 再次进行相同的部署,它就可以工作。

我已经尝试了在网上找到的以下建议。

  1. 确保服务已停止更改八达通的 powershell 脚本。这将等到服务停止后再重新配置它。我们怀疑服务没有正确关闭。

    #ensure that the service was stopped.
    Stop-Service $ServiceName
    do {
         Write-Host "Service not stopped yet...pausing execution for 200 ms"
         Start-Sleep -Milliseconds 200
       } until ((Get-Service $ServiceName).status -eq 'Stopped')
    
    
      & "sc.exe" config $service.Name binPath= $fullPath start= auto |       
    
  2. 增加服务onstart方法的超时时间

    protected override void OnStart(string[] args)
    {
        _eventLog.WriteEntry(_source, "Starting service...", EventLogEntryType.Information);
    
        //adding this to ensure there is enough time to start the service.
        this.RequestAdditionalTime(60000);
        new TaskFactory().StartNew(() => Start(_cancellationToken.Token), TaskCreationOptions.LongRunning);
        IsComplete = true;
    }
    

就像我提到的,如果您在错误发生后部署服务,那么它就可以工作。该错误仅在服务已 运行 宁相当长一段时间后发生,例如至少 2 小时。如果我每 10 分钟背靠背部署一次,错误不会自行出现。

请帮忙

好的,所以我设法找到了我的问题。我们的服务依赖于 WinHTTP Web Proxy Auto-Discovery Service ,该服务在一定空闲时间后由于某种原因停止。所以当我们部署我们的服务时,这个服务不是 运行 然后应该抛出一个异常。我们没有看到异常,因为它被 try catch 吞没了。

我将此服务添加为我们服务的依赖项,现在它每次都能正常工作。希望这对以后的人有所帮助。