我们有一个我们创建的服务(并在我们的一个服务器上运行),我想通过另一个程序控制它。我虽然服务有些困难,但希望有人可以提供帮助。
如果我尝试使用以下路线控制服务,它只适用于服务器上的用户:
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)所在的表单关闭,服务器就会在登录用户“已注销”时将其记录下来,并在此时关闭服务。我不知道为什么,但仍然试图调查它。
答案 0 :(得分:0)
让您正在运行的客户端代码模拟NT AUTHORITY\NetworkService
帐户,使其具有与您的服务相同的权限级别。该帐户The password is null(或者可能是空字符串,我猜两个)。有关如何在模拟该用户时运行代码的文档在WindowsIdentity.Impersonate中进行了演示。有关Impersonation/Delegation的更多信息。
此外,您可能希望在服务中实施handlers for all unhandled exceptions。可能会在try / catch块之外抛出异常,这就是为什么你的应用程序退出而你的附加调试器没有在catch块中设置的任何断点处中断。