3

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:

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

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

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

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?

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
        {
            _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:

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

Community
  • 1
  • 1
Tomek
  • 4,689
  • 15
  • 44
  • 52
  • You might also try posting your question on ServerFault.com – DOK Mar 08 '10 at 20:50
  • Since you now use WMI, you should probably wait for the Windows Management Instrumentation Service to start. Try adding it to dependent services on install time. – S.Skov Mar 09 '10 at 02:39

4 Answers4

8

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:

  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

Restart your computer and your service should now start correctly.

1

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 :-)

Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
  • @Joel Martinez I have added the try-catch but am getting the same exception (see my edit) – Tomek Mar 08 '10 at 21:53
0

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.

Timores
  • 14,439
  • 3
  • 46
  • 46
  • how can I find out which services my service depends on other than the Services applet? – Tomek Mar 08 '10 at 21:29
0

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.

Tomek
  • 4,689
  • 15
  • 44
  • 52