First some background on what I'm working on.

I have a particular service (Windows only) running on my computer, which is built using C#. Other than Control Panel > Management Tools > Services, I can also use C# programming to Start/Stop/Restart the said service. I use ServiceController class in System.ServiceProcess to do the above said operation.
I also have a web application, which uses web methods to perform various methods using this service. For instance, I might have MethodA() in my service, and I can access it using a web application (HTTP) and perform operations, send and receive data etc.

Now here's my requirement. I need to be able to Start/Stop/Restard this service by using a client computer, preferably using the above said web application. I can add a method like MethodA() to my web application if need be.
So, any idea on how I can get this to work? Also I need to know how to handle the admin previleges and such. Any thoughts are extremely valuable to me.

OK I can add a bit more. I created a web method, say MyWebMethod() and in it I start and stop the server using ServiceController class.

When I access this web method using a browser it gives the following error.


System.InvalidOperationException: <SERVICE_NAME> service cannot be opened in the computer <COMPUTER_NAME> ---> System.ComponentModel.Win32Exception: Access Denied

--- End of Stack Trace ---
System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
System.ServiceProcess.ServiceController.Start(String[] args)
System.ServiceProcess.ServiceController.Start()
Service.TestMethod() in c:\Program Files\MT\BS\BsWebService\App_Code\Service.cs: Line 522


Here Service.cs is where I have my web method, and at line 522 I have the code ServiceController.Start() which I use to start the windows service.

Anyone have any idea how I can obtain the necessary permission so the access will not be denied?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.