c# - 程序关闭后远程服务关闭

标签 c# windows-services

我们有一个我们创建的服务(并在我们的一个服务器上运行),我想通过另一个程序来控制它。不过,我在使用这项服务时遇到了一些困难,希望有人能提供帮助。

如果我尝试使用下面的路由控制服务,它也只适用于服务器上的用户:

ServiceController service = new ServiceController("MyService", "MyServer");

try
{
    if (service.Status == ServiceControllerStatus.Running)
    {
        teServiceStatus.BackColor = Color.LightGreen;
        teServiceStatus.Text = "Running";
        sbStartService.Enabled = false;
        sbStopService.Enabled = true;
    }
    else
    {
        teServiceStatus.BackColor = Color.Red;
        teServiceStatus.Text = "Stopped";
        sbStartService.Enabled = true;
        sbStopService.Enabled = false;
    }
}
catch (InvalidOperationException ioe)
{
    if (System.Diagnostics.Debugger.IsAttached)
        System.Diagnostics.Debugger.Break();
    else
    {
        teServiceStatus.Text = "Cannot Connect";
        teServiceStatus.BackColor = Color.Red;
    }
}
catch (Exception ex)
{
    if (System.Diagnostics.Debugger.IsAttached)
        System.Diagnostics.Debugger.Break();
    else
        ;
}

所以作为网络用户,我也作为用户在服务器上,当我运行我们想要控制该服务的程序时,我可以看到状态并启动和停止它,但没有其他人可以(除非我将它们添加到服务器,我不想这样做)。

我还可以使用下面的路由并在服务器上模拟用户:

//Establish connection to the Publishing Server with application credentials.
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
connectionOptions.EnablePrivileges = true;
connectionOptions.Username = "UserOnServer";
connectionOptions.Password = "UserPassword";
ManagementScope managementScope = new ManagementScope(@"\\MyServer\root\cimv2", connectionOptions);
managementScope.Connect();

ManagementPath path = new ManagementPath("Win32_Service");
ManagementClass services = new ManagementClass(managementScope, path, null);

foreach (ManagementObject service in services.GetInstances())
{
    try
    {
        if (service.GetPropertyValue("Name").ToString() == "MyService")
            if (service.GetPropertyValue("State").ToString().ToLower().Equals("running"))
            {
                teServiceStatus.BackColor = Color.LightGreen;
                teServiceStatus.Text = "Running";
                sbStartService.Enabled = false;
                sbStopService.Enabled = true;
            }
            else
            {
                teServiceStatus.BackColor = Color.Red;
                teServiceStatus.Text = "Stopped";
                sbStartService.Enabled = true;
                sbStopService.Enabled = false;
            }
    }
    catch (InvalidOperationException ioe)
    {
        if (System.Diagnostics.Debugger.IsAttached)
            System.Diagnostics.Debugger.Break();
        else
        {
            teServiceStatus.Text = "Cannot Connect";
            teServiceStatus.BackColor = Color.Red;
        }
    }
    catch (Exception ex)
    {
        if (System.Diagnostics.Debugger.IsAttached)
            System.Diagnostics.Debugger.Break();
        else
        {
            teServiceStatus.Text = "Cannot Connect";
            teServiceStatus.BackColor = Color.Red;
        }
    }
} 

这样一来,任何使用该程序的用户都可以启动/停止服务并查看其当前状态,但是,当程序关闭时,服务将停止(不知道这是否应该发生,但确实如此)。

那么我怎样才能让我的程序启动、停止并让任何使用它的人读取服务状态呢?我觉得我很接近,但显然,缺少一些东西。

更新 使用 ConnectionOptions 路由时,似乎一旦 ConnectionOptions(或 ManagementScope)所在的表单关闭,服务器就会将其记录为登录用户已“注销”,并在那时关闭服务.我不知道为什么,但仍在努力研究它。

最佳答案

让您运行的客户端代码模拟 NT AUTHORITY\NetworkService 帐户,以便它具有与您的服务相同级别的权限。 The password is null (或者可能是空字符串,我猜两个都试试)那个账户。 WindowsIdentity.Impersonate 中演示了有关如何在模拟该用户时运行代码的文档。 .有关更多信息,请参见 Impersonation/Delegation .

此外,您可能想要实现 handlers for all unhandled exceptions在您的服务中。有可能在您的 try/catch block 之外抛出异常,这就是为什么您的应用程序在您的附加调试器没有中断 catch block 中设置的任何断点的情况下退出的原因。

关于c# - 程序关闭后远程服务关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11400159/

相关文章:

c# - 验证被调用的函数参数是否具有特定值

c# - 如何安排 C# Windows 服务每天运行一个方法?

windows-services - Visual Studio 11 BETA 不支持 Windows Installer 项目

msbuild - 如何使用 MSBuild 远程启动/停止服务?

powershell - 当命令行中带有引号时,如何在Powershell中安装Windows服务?

c# - 无法使用 Visual Studio 2017 加载文件或程序集 'Microsoft.CodeAnalysis.LiveUnitTesting.Runtime, version= 1.7.0.0'

c# - WPF:无法在没有可怕视觉效果的情况下调整窗口大小

c# - Emgucv 自动裁剪检测到的形状

c# - LINQ:获取列表中具有最大属性值的记录

css - ABCPdf 没有嵌入新添加的自定义字体