Click here to Skip to main content
15,891,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to invoke a windows service installed in another machine in a web application?

I want to invoke a windows service installed in another machine in a web application. I am able to invoke the service in the same machine. I am working with asp.net with c#.
Code is as follows:
C#
#region StartService
//To start Windows Service exe created and installed in Services.msc
public bool StartService(string serviceName)
{
    ServiceController sc = new ServiceController(serviceName);

    try
    {
        if ((sc.Status.Equals(ServiceControllerStatus.Running)))
        {
            // Check whether the service if the current status is Running.
            return true;
        }
        else
        {
            sc.Start();
        }
    }
    catch
    {

    }
    return false;
}
#endregion

#region StopService
//To stop Windows Service exe created and installed in Services.msc
public bool StopService(string serviceName)
{
    bool startservice = false;
    ServiceController sc = new ServiceController(serviceName);

    try
    {
        if ((sc.Status.Equals(ServiceControllerStatus.Stopped)))
        {
            // Check whether the service if the current status is Stopped

            return true;

        }
         //Stop the Windows Service installed in services.msc
        else
        {
            sc.Stop();

        }
    }
    catch
    {

    }
    return false;
}
#endregion
Posted
Updated 13-Jul-15 22:26pm
v2

1 solution

You can use an alternative constructor to use another computers service. Have a look at ServiceController Constructor (String, String)[^]

Of course you need to have proper privileges for the target computer in order for this to work.
 
Share this answer
 
v2

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