Reputation: 2125
I'm developing an application to achieve work in background through a Windows Service. I've created the service thanks to the MSDN tutorial, and then I can Start or Stop it correctly.
My service also need to execute custom commands. Therefore, I've implemented the "OnCustomCommand" method in the service's class.
I can send custom execution commands with commands ID between 128 and 255, accordingly everything i've found on the net.
My problem is :
I can't execute command on the service when it's stopped. ExecuteCommand()
throws an System.InvalidOperationException
every time I call it on the serviceController
.
Cannot control <myservice> service on computer '.'
This does not happens when the service is running.
MSDN says
When you call ExecuteCommand, the status of the service does not change. If the service was started, the status remains Running. If the service was stopped, the status remains Stopped, and so on.
So I suppose this is actually possible to executeCommand
on stopped services.
My service is installed as LocalSystem service, and the serviceController
is run with administrator privileges.
Does anyone know how to resolve this?
Upvotes: 1
Views: 2133
Reputation: 15785
It would not be possible to ExecuteCommand against a stopped service. I believe what the documentation you're reading is telling you is that calling ExecuteCommand will not start the service if it is stopped. It is not saying that you will still be able to call the method; essentially no object exists to execute the method on. I think your best solution would be to find a way to determine if the service has stopped, if it has, restart it and then call ExecuteCommand. Otherwise, you could use kind of a "dirty hack" and attempt the call, if it fails, catch the exception and start the service and try a second time. By no means preferred, but possible.
EDIT:
Assuming that you're using a ServiceController you can use the Status property to determine if the service is running or not.
Upvotes: 3
Reputation: 44268
It's not possible to ExecuteCommands against a stopped service.
If it's not running, then it's not "there" to listen for the commands you send it.
Upvotes: 0