Click here to Skip to main content
15,924,196 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to call a windows service from asp.net i use below code but when i run it says :

Cannot control "Service Name" service on computer "Computer Name"

C#
//Asp.net
ServiceController service = new ServiceController("ABPS");
service.ExecuteCommand(1);

//Service
protected override void OnCustomCommand(int command)
        {
            base.OnCustomCommand(command);
            if (command == 1)
            {
                //Do something
            }//if
        }
Posted
Comments
[no name] 31-May-15 8:46am    
Calling a Windows Service does not make any sense. Is your service actually running on your server? And your web server account probably does not have permission anyway.
SajjadZare 31-May-15 11:16am    
Service running on server and website doesn't have permission
Sergey Alexandrovich Kryukov 31-May-15 12:25pm    
Why?
There is no such concept "run a service". Even if you want to start a service, this is a wrong idea. The services are designed to run permanently and communicate with other processes through their IPC interfaces.
—SA
SajjadZare 31-May-15 23:53pm    
I can stop and start service,but can't run command,if my website doesn't have permission to service,how can i give it permission??

1 solution

Please see my comment to the question. Your ASP.NET does not have permissions to use the service controller? This is done for some good reasons. Normally a Web application is executed in a sandboxed environment which prevents accessing the rest of the host system. This is safety-critical architectural feature of the Web server.

The whole idea is wrong. Even though some application can start and stop a service through the service controller, this is not how the whole thing is designed. The service is really designed to be executed permanently (most usually, it is automatically started) and, very typically, its threads spend most time in a wait state, not wasting any CPU time, until a service request is received by some IPC method which supports thread blocking with wait state. This could be sending some block of data through a socket, named pipe, etc. The request wakes the thread up, the service does its work and sleeps again. That's why stopping and starting it is never needed.

The application which are started and stopped on requests are usually not services but regular applications. Such applications could be on your site, which would not cause your permission problem. Better yes, it could be just a part of you code behind of some of your pages. What, this cannot do what a service could do? But I already explained: the Web service is designed to protect its host system from your artistry on the site… So, you may have a chance to get more specific advice if you explain the ultimate purpose of you activity.

—SA
 
Share this answer
 
Comments
SajjadZare 1-Jun-15 6:56am    
I can stop and start service,but can't run command,if my website doesn't have permission to service,how can i give it permission??
Sergey Alexandrovich Kryukov 1-Jun-15 11:27am    
I told you that you don't need to stop and start the service. What do you mean by "run command"?
—SA
SajjadZare 1-Jun-15 23:40pm    
see my question,I have a method in service "OnCustomCommand" that want to call it from website
Sergey Alexandrovich Kryukov 2-Jun-15 0:23am    
I know. (Sorry, in my previous comment, asking "what do you mean?..", I forgot about it.) This is a simplified interface to the service, sent using ExecuteCommand statement in a ServiceController. I suggest you don't use it; use your custom IPC interface instead. Don't deal with access denied.

But... it's not quite clear to me where the exception was thrown: in the ServiceController call, or in the service itself?

—SA
SajjadZare 6-Jun-15 1:59am    
Can you give me an example or a link for custom IPC interface ?

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