Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hello.
 
I have InvalidOperationException for an unknown reason to me. The code worked fine in an ad hoc network, but when you upgrade to Active Directory, I got this problem.
 
I have sufficient access rights. Windows tool "Services" give me the opportunity to change the status of services. However, I am InvalidOperationException when you try to do this with ServiseController.
 

a simple example:
ServiceController sc = new ServiceController(@"SomeService", @"SomeMachine");
Here I get an instance of service with all members values.
 
sc.Refresh();
if (sc.Status == ServiceControllerStatus.Runing)
update and read state as well.
 
sc.Stop();
But (and any other attempt to change the state) are getting an InvalidOperationException.
 
Can you please tell what are the possible reasons for this execution of the code wiht fine working Services tool.
 

Thank you very much.
Posted 29-Jun-12 3:40am
Labilis341

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I believe the issue here is one of permissions. I know you stated you have the necessary permissions, but that doesn't mean your application does. First, if you are running on Windows 7, you will need to make sure you are running your app as administrator (the mode, not just logged in as admin).
 
Next, you might need to look at impersonation. It looks like this Stack Overflow question describes a similar issue and it has a listed solution:
 
http://stackoverflow.com/questions/2996282/how-to-remotely-control-a-windows-service-with-servicecontroller[^]
 
Here is another one with code listed:
 
http://stackoverflow.com/questions/6866104/c-sharp-service-status-on-remote-machine[^]
  Permalink  
v2
Comments
Labilis at 2-Jul-12 12:50pm
   
You are right. It was in Windows 7 UAC. Run as administrator gave me the necessary authority. Thank you and I apologize for that.
But after there was another difficulty associated with the need to request necessary execution mode.
All described variants describe the manipulation of the application manifest.
(like this http://stackoverflow.com/questions/2818179/how-to-force-c-sharp-app-to-run-as-administrator-on-windows-7)
And no more ways I have not found
But change the requestedExecutionLevel in manifest on any different from asInvoker gives me the error "does not support the request execution level "
Could you tell me, please, how i can solve this?
Labilis at 2-Jul-12 16:16pm
   
Found. Thank you any way.
Tim Corey at 2-Jul-12 16:25pm
   
Great. Glad you figured it out. If you wouldn't mind, could you please accept this answer so that others that look at this question later know that it isn't still an issue? Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 717
1 ChauhanAjay 135
2 Maciej Los 130
3 ashok rathod 90
4 ChintanShukla 90
0 Sergey Alexandrovich Kryukov 10,200
1 OriginalGriff 6,987
2 CPallini 5,850
3 George Jonsson 4,015
4 Gihan Liyanage 3,266


Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 29 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100