代码之家  ›  专栏  ›  技术社区  ›  Aaron M

引发异常时重新启动服务

  •  3
  • Aaron M  · 技术社区  · 16 年前

    我正在编写一个需要全天候运行的Windows服务。这是一个非常简单的服务,它监视一个目录,在该目录中文件被放入并处理这些文件。如果引发未处理的异常,我需要重新启动服务。

    在发生未处理的异常时,是否有方法让服务自行重新启动?

    6 回复  |  直到 15 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

    服务小程序具有许多不同的恢复功能:

    Services Recovery

    它可以对第一次、第二次和后续故障采取不同的操作:

    • 在可配置的延迟之后重新启动服务
    • 运行程序(传递命令行参数,可能包括失败计数)
    • 重新启动计算机(在可配置的延迟之后,并发送特定的消息)

    运行的程序应该能够在事件日志中查看失败的原因(特别是如果您记录了失败的原因),因此如果异常是不可恢复的,那么应该能够禁用服务。

    当然,同时,服务应该记录正在发生的事情,这应该使任何管理工具都能够将正在发生的事情通知操作人员。

    我同意,您可能不应该将“第三个和后续的”配置为“重新启动服务”,或者您可以在一个循环中结束。

        2
  •  2
  •   JustinD    16 年前

    您是否尝试过使用服务条目的“恢复”选项卡-您可以设置失败规则,包括“重新启动服务”-默认情况下,此选项处于“无操作”状态

        3
  •  1
  •   Mike Z    16 年前

    如果你想的话,可以通过编程来完成,这段代码不是我写的。我正在发布指向包含源/二进制文件的作者代码项目页面的链接。在链接下面,我解释了如何实现作者代码。

    http://www.codeproject.com/KB/install/sercviceinstallerext.aspx

    1. 添加对dll的引用。

    2. 在记事本中打开projectinstaller.designer.vb
      在InitializeComponent Sub中
      变化
      Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller
      Me.ServiceInstaller1 = New System.ServiceProcess.ServiceInstaller

      me.serviceProcessInstaller1=新建system.serviceProcess.serviceProcessInstaller
      Me.ServiceInstaller1 = New Verifide.ServiceUtils.ServiceInstallerEx

    3. 在projectinstaller.designer.vb中使用友元声明
      变化
      Friend WithEvents ServiceProcessInstaller1 As System.ServiceProcess.ServiceProcessInstaller
      Friend WithEvents ServiceInstaller1 As System.ServiceProcess.ServiceInstaller

      作为System.ServiceProcess.ServiceProcessInstaller与Events ServiceProcessInstaller的好友1
      Friend WithEvents ServiceInstaller1 As Verifide.ServiceUtils.ServiceInstallerEx

    4. 变化
      Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.ServiceProcessInstaller1, Me.ServiceInstaller1})

      Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.ServiceInstaller1, Me.ServiceProcessInstaller1})

    5. 导入ProjectInstaller.vb上的命名空间

    6. 在ProjectInstaller.vb中,在调用了初始化组件函数后的公共子新函数
      添加
      'Set Reset Time Count - This Is 4 Days Before Count Is Reset
      ServiceInstaller1.FailCountResetTime = 60 * 60 * 24 * 4
      'ServiceInstaller1.FailRebootMsg = "Houston! We have a problem"

      'Add Failure Actions
      ServiceInstaller1.FailureActions.Add(New FailureAction(RecoverAction.Restart, 60000))
      ServiceInstaller1.FailureActions.Add(新建FailureAction(recoverAction.restart,60000))。
      ServiceInstaller1.FailureActions.Add(New FailureAction(RecoverAction.None, 3000))

      ServiceInstaller1.StartOnInstall = True

    7. 构建安装程序并安装。沃伊拉

        4
  •  0
  •   theGecko    16 年前

    将服务代码包装在一个运行程序中,该运行程序可以捕获任何错误并重新启动服务。

        5
  •  0
  •   Dan Diplo    16 年前

    最好的方法是将try/catch块包装在服务中可以使用的方法周围 负担得起 让抛出异常。

    但是,可能会引发严重的异常,导致服务立即停止。不要忽视这些!在这些情况下,处理异常,记录它,通过电子邮件发送它,然后重新显示它。这样,您将被告知异常已经发生,并知道出了什么问题。然后您可以修复问题并手动重新启动服务。

    忽略它可能会导致系统中一个你不知道的重大故障。在CPU/RAM上,如果服务停止,然后重新启动,然后停止,代价也可能非常昂贵。 无穷大 .

        6
  •  0
  •   CoreTech    15 年前

    正如“JohnSaunders”和“TheGecko”所建议的,您可以监视服务并在服务失败时重新启动它。内置的Windows服务恢复功能将为您带来很大的帮助,但是如果您发现需要一些更高级的功能(例如CPU占用和挂起检测),请查看 Service Protector . 它旨在让您的重要Windows服务全天候运行。

    祝你好运!