【问题标题】:"External component has thrown an exception"“外部组件引发的异常”
【发布时间】:2013-04-19 00:35:10
【问题描述】:

我在 C# 中有一个 Windows 服务。运行1-2天后(不具体),服务停止(它实际上并没有停止,但它没有处理任何东西。服务状态仍然是“已启动”,我需要手动重新启动服务,然后它才能再次工作)。

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

我希望服务继续运行而不会停止,因为在生产中,除非特定功能停止,否则没有人会检查服务是否正在启动/处理。

我已经准备好了所有的 TRY..CATCH。

任何建议,即使发生上述错误,我如何确保服务继续?

【问题讨论】:

  • 首先,记录整个异常。记录ex.ToString(),而不仅仅是ex.Message。几乎可以肯定有一个内部异常,告诉你到底发生了什么,在哪里。

标签: c# .net windows-services


【解决方案1】:

【讨论】:

  • 感谢您的回复.. 但我已经在帖子中提到 try..catch 已经到位...有更好的建议吗?
【解决方案2】:

你可能想尝试在你的 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 失败,您可以一起停止您的服务。代码示例here

【讨论】:

    猜你喜欢
    • 2011-07-16
    • 2015-12-05
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多