Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
4.00/5 (1 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:
C#
ServiceController sc = new ServiceController(@"SomeService", @"SomeMachine");

Here I get an instance of service with all members values.

C#
sc.Refresh();
if (sc.Status == ServiceControllerStatus.Runing)

update and read state as well.

C#
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

1 solution

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[^]
 
Share this answer
 
v2
Comments
Labilis 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 <level>"
Could you tell me, please, how i can solve this?
Labilis 2-Jul-12 16:16pm    
Found. Thank you any way.
Tim Corey 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900