外部组件抛出了一个异常

本文关键字:一个 异常 组件 外部 | 更新日期: 2023-09-27 18:10:44

我有一个c#的windows服务。运行1-2天后(不确定),服务停止(它实际上没有停止,但它没有处理任何事情)。服务状态仍然是"已启动",我需要手动重启服务,然后它再次工作)。

记录的异常是"外部组件抛出异常"。这发生在第三方控件中。

我想让服务不间断地继续,因为在生产环境中没有人会检查服务是否启动/处理,除非特定的功能停止。

我有所有的TRY..CATCH在适当的地方。

有什么建议,我怎样才能确保服务继续,即使上述错误发生?

外部组件抛出了一个异常

请使用此try-catch结构。

try{
}catch{
}
http://www.java2s.com/Code/CSharp/Language-Basics/Usethecatchallcatchstatement.htm

您可能想在catch块中尝试这样做:

public static void RestartService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
    int millisec1 = Environment.TickCount;
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
    service.Stop();
    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
    // count the rest of the timeout
    int millisec2 = Environment.TickCount;
    timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));
    service.Start();
    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
  }
  catch
  {
    // ...
  }
}

如果RestartService失败,你可以一起停止你的Service。这里的代码示例