C# 处理服务启动异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/158772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:08:20  来源:igfitidea点击:

Handle exception on service startup

提问by C. Ross

I'm writing a series of Windows services. I want them to fail if errors are thrown during startup (in OnStart()method). I had assumed that merely throwing an error in OnStart()would do this, but I'm finding that instead it "Starts" and presents me with a message stating "The service has started, but is inactive. Is this correct?" (Paraphrase). How do I handle the error so it actually fails to start the service?

我正在编写一系列 Windows 服务。如果在启动期间(在OnStart()方法中)抛出错误,我希望它们失败。我原以为仅仅抛出一个错误OnStart()就可以做到这一点,但我发现它“开始”并显示一条消息,指出“服务已启动,但处于非活动状态。这是正确的吗?” (释义)。如何处理错误,使其实际上无法启动服务?

采纳答案by Steven A. Lowe

if you are running .NET 2.0 or higher, you can use ServiceBase.Stopto stop the service from OnStart. Otherwise call Stop from a new thread.

如果您运行的是 .NET 2.0 或更高版本,您可以使用ServiceBase.Stop从 OnStart 停止服务。否则从新线程调用 Stop 。

ref [devnewsgroups] (http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic50404.aspx)

参考 [devnewsgroups](http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic50404.aspx

(news group gone)

(新闻组不见了)

回答by StingyHyman

Move all of your startup logic to a separate method, and Throw exceptions (or call OnStop) from that seperate method.

将所有启动逻辑移动到一个单独的方法,并从该单独的方法抛出异常(或调用 OnStop)。

OnStart has some oddities when starting up. I have found that if OnStart() has no more than one line in it, then I dont get the "The service started and then stopped.Some services stop automatically if they have no work to do" message, and thrown exceptions will terminate the process and log to the app event log.

OnStart 在启动时有一些奇怪的地方。我发现如果 OnStart() 中只有一行,那么我不会收到“服务启动然后停止。如果没有工作要做,一些服务会自动停止”消息,抛出的异常将终止处理并记录到应用程序事件日志。

Also with the seperate startup method, you can use a technique like this to debug it without attaching. http://www.codeproject.com/KB/dotnet/DebugWinServices.aspx

同样使用单独的启动方法,您可以使用这样的技术来调试它而无需附加。http://www.codeproject.com/KB/dotnet/DebugWinServices.aspx

回答by Sean

If the main thing you want is for the Services window to report that there was an error, from what I've tried (.NET3.5 on Windows 7), the only way to do this is by setting the ExitCode. I recommend setting it to 13816, since this results in the message, "An unknown error has occurred." See the windows error codes.

如果您希望“服务”窗口报告出现错误,那么根据我的尝试(Windows 7 上的 .NET3.5),唯一的方法是设置ExitCode. 我建议将其设置为 13816,因为这会导致消息“发生未知错误”。请参阅Windows 错误代码

The sample below accomplishes three things.

下面的示例完成了三件事。

  1. Setting ExitCode results in a useful message for the end-user. It doesn't affect the Windows Application log but does include a message in the System log.
  2. Calling Stop results in a "Service successfully stopped" message in the Application log.

  3. throwing the exception results in a useful log entry in the Application log.

  1. 设置 ExitCode 会为最终用户生成有用的消息。它不会影响 Windows 应用程序日志,但会在系统日志中包含一条消息。
  2. 调用停止会导致应用程序日志中出现“服务已成功停止”消息。

  3. 抛出异常会在应用程序日志中生成一个有用的日志条目。

protected override void OnStart(string[] args) {
    try {
        // Start your service
    }catch (Exception ex) {
        // Log exception
        this.ExitCode = 13816;
        this.Stop();
        throw;
    }  
}