为什么我的 .NET Windows 服务有时不会自动启动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2404569/
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-09-15 14:05:51  来源:igfitidea点击:

Why does my .NET Windows service not start automatically sometimes?

.netwindowsservicesystem.management

提问by Tomek

I have modified a working Windows service that had always been starting beforehand. After adding the System.Management reference it now sometimes will not start automatically. I get the following error:

我修改了一个一直在启动的可用 Windows 服务。添加 System.Management 引用后,它现在有时不会自动启动。我收到以下错误:

Service cannot be started. System.Runtime.InteropServices.COMException (0x80010002): Call was canceled by the message filter. (Exception from HRESULT: 0x80010002 (RPC_E_CALL_CANCELED))

服务无法启动。System.Runtime.InteropServices.COMException (0x80010002):消息过滤器取消了调用。(来自 HRESULT 的异常:0x80010002 (RPC_E_CALL_CANCELED))

I found another post here on SO with someone having the same issue.

我在 SO 上找到了另一个帖子,有人遇到了同样的问题。

Why won't my .Net Windows service start automatically after a reboot?

为什么我的 .Net Windows 服务在重新启动后不会自动启动?

However, the proposed solution was to have the service start after the services it depends on have started. However, when I go to the Dependencies tab for my service, I see:

但是,建议的解决方案是在它所依赖的服务启动后启动该服务。但是,当我转到我的服务的依赖项选项卡时,我看到:

alt text

替代文字

Should I just use the workaround method of putting the thread to sleep, or is there a more proper way of getting this service to start correctly? Is this happening because .NET has not started before my service starts?

我应该使用让线程休眠的变通方法,还是有更正确的方法让这个服务正确启动?这是因为 .NET 在我的服务启动之前没有启动吗?

Thanks,

谢谢,

Tomek

托梅克

EDIT: I have added a try-catch statement to catch the exception. Here is the code that I added to the OnStart() method of my service (which is where the exception is being thrown)

编辑:我添加了一个 try-catch 语句来捕获异常。这是我添加到我的服务的 OnStart() 方法中的代码(这是抛出异常的地方)

        try
        {
            _watcher = new ManagementEventWatcher(query);
            _watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
            _watcher.Start();  
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry("Could not create Sleep/Resume watcher" + ex.Message);
        }

The service does start now but without the functionality that I have added. I am new to .NET, but I took the watcher code from a sample I found online, so I am pretty sure it is correct. The Event Log displays the same exception:

该服务现在启动,但没有我添加的功能。我是 .NET 的新手,但我从网上找到的示例中获取了观察者代码,所以我很确定它是正确的。事件日志显示相同的异常:

Could not create Sleep/Resume watcher Call was canceled by the message filter. (Exception from HRESULT: 0x80010002 (RPC_E_CALL_CANCELED))

无法创建睡眠/恢复观察程序呼叫被消息过滤器取消。(来自 HRESULT 的异常:0x80010002 (RPC_E_CALL_CANCELED))

采纳答案by Tomek

I ended up using Thread.Sleep(10000) right before I create the ManagementEventWatcher (before the try statement)

在创建 ManagementEventWatcher 之前(在 try 语句之前),我最终使用了 Thread.Sleep(10000)

It is kind of a workaround, but it did fix the problem.

这是一种解决方法,但确实解决了问题。

回答by Jonathan Persson

I've had this problem myself and apparently it only occurs on Windows XP (not on Vista or Win 7). To fix this you need to add a dependency to the Windows Management Instrumentation service. Adding this dependency to your existing service is done in three simple steps:

我自己也遇到过这个问题,显然它只发生在 Windows XP 上(不在 Vista 或 Win 7 上)。要解决此问题,您需要向 Windows Management Instrumentation 服务添加依赖项。将此依赖项添加到您现有的服务中,只需三个简单的步骤:

  1. Open the command prompt (Windows+R -> cmd -> enter)
  2. Type: sc config "NAME_OF_YOUR_SERVICE" depend= winmgmt
  3. Press enter, you should see: [SC] ChangeServiceConfig SUCCESS
  1. 打开命令提示符(Windows+R -> cmd -> enter)
  2. 类型:sc 配置“NAME_OF_YOUR_SERVICE”depend= winmgmt
  3. 按回车,你应该看到: [SC] ChangeServiceConfig SUCCESS

Restart your computer and your service should now start correctly.

重新启动您的计算机,您的服务现在应该可以正确启动。

回答by Joel Martinez

The problem isn't with the service itself, it's that the new component you added a reference to is throwing an unhandled exception (Call was canceled by the message filter). Troubleshoot that error message with whatever code you added, and/or put in better error handling so that an error in that component will not bubble up to the top and cause the service to stop :-)

问题不在于服务本身,而是您添加引用的新组件引发了未处理的异常(消息过滤器取消了呼叫)。使用您添加的任何代码解决该错误消息,和/或进行更好的错误处理,以便该组件中的错误不会冒泡到顶部并导致服务停止:-)

回答by Timores

If you depend on another service, us sc.exe to configure your service to start after the dependency. This cannot be done through the Services applet.

如果您依赖其他服务,我们可以使用 sc.exe 配置您的服务在依赖后启动。这不能通过服务小程序完成。