如何调用Web应用程序中另一台机器上安装的Windows服务? [英] How to invoke a windows service installed in another machine in a web application?

查看:86
本文介绍了如何调用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构造函数(字符串,字符串) [< a href =https://msdn.microsoft.com/en-us/library/ssbk2tf3(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]



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


这篇关于如何调用Web应用程序中另一台机器上安装的Windows服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆