views:

403

answers:

2

I'm trying to access a windows service (also created by me) through a WinForms application using ServiceController:

service = new System.ServiceProcess.ServiceController("MyService")

The service returns OK and I can see the status via myservice.Status.

But when i try to start (or stop) it, it returns the error:

Unable to open service MyService in machine '.'

I'm Administrator and the service process is running as NetworkService.

I'm new at .net and windows applications and I can't seem to further debug it.

+1  A: 

Are you sure you're using the service name and not the display name?

Double check that you're a local administrator on the box.

Is the service running on the local machine? I'm not sure why you're seeing the host name '.'

EDIT:

I read online that the following code throws an InvalidOperationException if Test Service is not a valid service name:

var sc = new ServiceController("Unknown Service");
string name = sc.DisplayName;

The documentation for this constructor doesn't indicate that it would throw an exception, so my guess is that you have will need to use the service name. Any exception you see after that is actually a real error.

Hope that's clearer.

Drew Noakes
I think i'm using the right one, it's "MySMS Service". The actual ServiceName in Service properties ("MySMS_Service") errors on service.Status so i figured "MySMS Service" was correct because Status "Stopped" is returned.Yes, my user account has Administrator rights.Yes, the service is running on the local machine, i haven't set MachineName property so it should default to the local machine.
Trouts
I've updated the answer. I believe you need to use the service name, not the display name.
Drew Noakes
ok, to be sure i used ServiceController.GetServices() and cycled through all services - the serviceName is indeed "MySMS Service". - I can Status and get DisplayName, etc of it - but only when i try to Start or Stop it returns "Unable to open service MySMS Service in machine ‘.’."
Trouts
What is the type of the exception you're seeing? Also, what credentials are being used by the code you're running? Are you sure it's using your credentials?
Drew Noakes
One more question -- does it start if you use the `services.mmc` or `sc.exe`?
Drew Noakes
exception type is System.InvalidOperationException. How do i check what credentials it is using? - If i use 'sc start "MySMS Service"' i get "Access Denied", if i use services.msc it works.
Trouts
Strange that it works in one and not the other. In code, you could use `Environment.UserDomainName` to access who it thinks the active user is. You could also use `Thread.CurrentPrincipal` to get thread-level security information.
Drew Noakes
Is there anything in the event log that provides extra information about what's wrong here?
Drew Noakes
The service is under user "4HSA" (machine owner?) with Thread "System.Security.Principal.GenericPrincipal", application is under user "QUALIDADE" (i have no ideia 'who' it is) with same Thread level. I'm logged in as "9tree"... - I've put the server to "Everyone" on install, could that have anything to do with it? - There's no further information in the event log.
Trouts
Hard to say based off those names. I'd investigate what each of those labels mean, seeing as the only reason I can see for an InvalidOperationException is that your process doesn't have sufficient security on the machine to start/stop the service. You might add QUALIDADE (is that a proper noun or a term in another language?) as an administrator on the machine and see if that helps.
Drew Noakes
QUALIDADE is probably a user on the machine, what i don't understand is why the app does not run on my user (9tree) as i am the one starting it. I want users to install it and not having to worry with these type of permission issues. Is there anyway to force the application to be launched with administrative rights in the code?
Trouts
A: 

I've finally been able to workaround the problem by setting execution level in the app.manifest (project->new item->application manifest)

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Now the control application requires administrator priviledges and therefore can start/stop the service without a problem.

Still, i believe i can do it in some other way using impersonate via code or maybe somehow lowering the service run level.

Before launching the application i'll have to review this and i'll post my findings here then.

Thanks to Drew for the guidance throughout debugging.

Regards

Trouts