为什么我的 .NET Windows 服务有时不会自动启动? [英] Why does my .NET Windows service not start automatically sometimes?

查看:21
本文介绍了为什么我的 .NET Windows 服务有时不会自动启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

服务无法启动.System.Runtime.InteropServices.COMException(0x80010002): 呼叫被取消消息过滤器.(例外来自结果:0x80010002(RPC_E_CALL_CANCELED))

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

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

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

为什么我的 .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:

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

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?

谢谢,

托梅克

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

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
        {
            _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);
        }

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

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:

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

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

推荐答案

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

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

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

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

这篇关于为什么我的 .NET Windows 服务有时不会自动启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆