我正在Windows Server 2012 R2上设置Windows服务。当我将服务安装到我的计算机(win10 Entr。)时,它工作得很好。我可以从随服务安装的UI启动和停止服务。但是,如果我将服务和UI安装到Windows Server 2012 R2,我的日志会向我显示它无法从UI停止服务。在UI中,当我点击“停止”按钮时,下面的代码可以正常工作。
ServiceController sc = new ServiceController();
sc.ServiceName = "xxService";
if (sc.Status == ServiceControllerStatus.Running)
{
try
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
return true;
}
catch (InvalidOperationException e)
{
return false;
}
}
我想知道我是否必须为服务器端处理一些事情。看起来上面的代码无法停止服务。
很抱歉花时间提出这样的问题。我只是以管理员身份运行UI,然后才能运行。