如何调用Web应用程序中另一台机器上安装的Windows服务?

如何调用Web应用程序中另一台机器上安装的Windows服务?

问题描述:

如何调用Web应用程序中另一台机器上安装的Windows服务?



我想调用安装在Web应用程序中另一台机器上的Windows服务。我可以在同一台机器上调用该服务。我正在使用带有c#的asp.net。

代码如下:

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:

#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

您可以使用替代构造函数来使用其他计算机服务。看一下 ServiceController构造函数(字符串,字符串) [ ^ ]



当然,您需要拥有目标计算机的适当权限才能使其正常工作。
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.