【问题标题】:Windows, Start service System.InvalidOperationException: Cannot start service on computer '.' Access in Denied(Running as Admin)Windows,启动服务 System.InvalidOperationException:无法在计算机“。”上启动服务拒绝访问(以管理员身份运行)
【发布时间】:2016-09-15 17:51:36
【问题描述】:

我正在尝试使用以下代码启动服务。这适用于 99% 的机器,但我在用户机器上遇到了这个问题。能够重现此错误或为什么会发生此问题的任何帮助。

            ServiceController sc = new ServiceController(name);

            if (sc.Status == ServiceControllerStatus.Running ||
                    sc.Status == ServiceControllerStatus.StartPending)
            {
                sc.WaitForStatus(ServiceControllerStatus.Running);
                Logger.Info("Service already running");
                return true;
            }
            sc.Start();

我收到的错误是

System.InvalidOperationException:无法在计算机上启动服务 '.'。 ---> System.ComponentModel.Win32Exception: 访问被拒绝

我正在以管理员权限运行

在创建服务时,我还运行 sc sdset 命令以使非管理员进程停止服务启动。

【问题讨论】:

  • 我的错误发生在 OnAfterInstall 事件中。 var sc = new ServiceController(si.ServiceName); if (sc.Status == ServiceControllerStatus.Stopped) { sc.Start(); }
  • @KirstenGreed 你能发布堆栈跟踪吗?
  • 我正在使用命令 installutil.exe myservice.exe 我如何获得堆栈跟踪?
  • 我创建了一个答案,以便更好地格式化我发现的内容。

标签: c# windows windows-services


【解决方案1】:

social msdn 中有一个完整的主题。许多用户仍然存在此问题,并且您似乎没有足够的权限来启动该服务,在这种情况下,您必须将服务更改为管理员帐户

确保通过以下方式将服务设置为本地帐户

  • 右键单击属性(在 Services.msc 面板中)。
  • 选择登录选项

然后再次检查它是否正常工作。

【讨论】:

    【解决方案2】:

    我假设您正在尝试实现类似于this 的场景,即安装服务并自动启动它。

    假设您已确定您确实以管理员身份运行,即管理员命令提示符或以管理员身份运行。

    另外,假设您已重新启动机器以确保确实删除了旧版本的服务,因为您已多次尝试安装/卸载您的服务。

    错误Access is denied 本质上意味着运行它的用户没有访问权限。既然您说您以管理员身份运行,那么即使管理员也可能无权启动该服务。可能您处于锁定环境中(可能通过组策略),其中只有域管理员是“最强大的”!

    下一步是调查您的服务拥有的权限。一个有用的工具是:SubInAcl

    https://ss64.com/nt/subinacl.html

    显示或修改文件和文件夹权限、所有权和域的访问控制条目 (ACE)。

    SubInAcl /service "your service name"

    上面的命令不好用!你需要从微软网站download它。

    另一个有用的工具是SCcommand。这通常默认情况下可用。

    服务控制 - 创建、启动、停止、查询或删除任何 Windows SERVICE。

    SC sdshow "your service name"

    将提供许可的详细信息。

    这将为您提供有助于进一步调查的数据。

    您也可以使用此命令启动/停止服务。您也可以尝试使用此工具来检查您在使用此工具时是否遇到相同的异常。

    以下服务器故障问题提供了有关设置服务权限的一些详细信息

    https://serverfault.com/questions/187302/how-do-i-grant-start-stop-restart-permissions-on-a-service-to-an-arbitrary-user

    如果您想查看来自installutil 的堆栈跟踪,您可以使用/ShowcallStack 选项

    https://docs.microsoft.com/en-us/dotnet/framework/tools/installutil-exe-installer-tool

    /ShowCallStack
    如果在安装过程中的任何时候发生异常,则将调用堆栈输出到日志文件。

    【讨论】:

      【解决方案3】:

      这不是答案,是我对这个问题的进一步探索。

      这是日志

      Running a transacted installation.
      
      Beginning the Install phase of the installation.
      See the contents of the log file for the 
      
      D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.exe assembly's progress.
      The file is located at D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.InstallLog.
      
      An exception occurred during the Install phase.
      System.InvalidOperationException: An exception occurred in the OnAfterInstall event handler of Flight.ServiceHost.Installation.
         at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
         at Flight.ServiceHost.Installation.Install(IDictionary stateSaver) in D:\devnet10\Flight\Flight.ServiceHost\Installation.cs:line 36
         at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
         at System.Configuration.Install.AssemblyInstaller.Install(IDictionary savedState)
         at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
         at System.Configuration.Install.TransactedInstaller.Install(IDictionary savedState)
      The inner exception System.InvalidOperationException was thrown with the following error message: Cannot start service PreFlight on computer '.'..
         at System.ServiceProcess.ServiceController.Start(String[] args)
         at System.ServiceProcess.ServiceController.Start()
         at Flight.ServiceHost.Installation.OnAfterInstall(IDictionary savedState) in D:\devnet10\Flight\Flight.ServiceHost\Installation.cs:line 49
         at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
      The inner exception System.ComponentModel.Win32Exception was thrown with the following error message: Access is denied.
      
      
      The Rollback phase of the installation is beginning.
      See the contents of the log file for the D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.exe assembly's progress.
      The file is located at D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.InstallLog.
      
      The Rollback phase completed successfully.
      
      The transacted install has completed.
      

      [更新]

      我应该提到我正在运行 WIndows 10 我设法使用an installer project 创建了一个安装程序,它确实有效。

      【讨论】:

      • 能否也添加日志文件的内容? (D:\devnet10\Flight\Flight.ServiceHost\bin\Debug\flight.servicehost.InstallLog)
      • 是一样的。
      猜你喜欢
      • 2015-09-26
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 2011-05-15
      相关资源
      最近更新 更多