Why doesn't my .NET Windows service start automatically sometimes?

I changed the working Windows service, which always started in advance. After adding a System.Management link, it sometimes does not start automatically. I get the following error:

The service cannot be started. System.Runtime.InteropServices.COMException (0x80010002): The call has been canceled by the message filter. (Exception from HRESULT: 0x80010002 (RPC_E_CALL_CANCELED))

I found here another message saying that someone has the same problem.

Why doesn't my automatic windows service start automatically after reboot?

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

alt text

Should I just use a workaround method that puts the thread to sleep, or is there a better way to start this service? Is this because .NET did not start before my service started?

Thank,

Tomek

EDIT: I added a try-catch statement to catch the exception. Here is the code that I added to the OnStart () method of my service (in which an exception was 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 is starting now, but without the functionality that I added. I'm new to .NET, but I took the observer code from a sample that I found on the Internet, so I'm sure it is correct. The event log displays the same exception:

Failed to create Sleep / Resume call session. Disabled message filter. (Exception from HRESULT: 0x80010002 (RPC_E_CALL_CANCELED))

+3
4

Thread.Sleep(10000) ManagementEventWatcher ( try)

, .

0

, , -, Windows XP ( Vista Win 7). , Windows Management Instrumentation. :

  • (Windows + R → cmd → enter)
  • : sc config "NAME_OF_YOUR_SERVICE" depend = winmgmt
  • enter, : [SC] ChangeServiceConfig

, .

+8

, , , , ( ). / , : -)

+1

, sc.exe . .

0

Source: https://habr.com/ru/post/1736002/


All Articles