2015-03-02 104 views
0

我用下面的代码来检查我的Windows服务的状态:C#检查Windows服务状态

ServiceController sc = new ServiceController("service name", "service location"); 
地把它我的服务驻留在服务器名称的服务定位嗯

。在我的测试环境中,我有服务和门户(IIS),我在同一台服务器上检查我的服务,但工作正常,但在我的生产环境中,该服务位于与我的门户网站IIS不同的服务器上。

我的代码无法检查状态。我确定这是一个许可问题,但我尝试了很多这样的工作,但没有用。

我的问题是:什么“类型的权限”给予什么“用户”或“机器名称”?请帮忙。

回答

0

感谢所有反正我用另一种方式,通过WMI和它的工作:

ConnectionOptions op = new ConnectionOptions(); 
     op.Username = ""; 
     op.Password = ""; 
     ManagementScope scope = new ManagementScope([email protected]"\root\cimv2", op); 
     scope.Connect(); 
     ManagementPath path = new ManagementPath("Win32_Service"); 
     ManagementClass services = new ManagementClass(scope, path, null); 
     foreach (ManagementObject service in services.GetInstances()) 
     { 
      if (service.GetPropertyValue("Name").ToString().ToLower().Equals("serviceName")) 
      { 
       if (service.GetPropertyValue("State").ToString().ToLower().Equals("running")) 
       { 
        //do something 
       } 
       else 
       { 
        //do something 
       } 
      } 
     } 
0

您的IIS门户是模仿吗?它似乎确实如此。因此,您正在陷入Kerberos constrained delegation('双跳')。你不能'修复'这个。您必须要求域管理员为您的IIS应用程序启用和配置受限委派。

另一种方法是而不是在您的IIS中模仿。在这种情况下,您需要为您的门户应用程序池授予适当的权限。 SC_MANAGER_CONNECT所需的权限。阅读更多信息,请致电KB179249: Access to the Service Control Manager。显然,应用程序池必须是域帐户,并且IIS和目标服务主机应位于同一个域中,或者位于具有信任关系的域中。