【问题标题】:Windows Service - UnauthorizedAccessException Error when Installing Service (C#)Windows 服务 - 安装服务时出现 UnauthorizedAccessException 错误 (C#)
【发布时间】:2019-04-24 05:28:07
【问题描述】:

所以我尝试在 Visual Studio 2017 中使用 C# 创建服务,但在尝试安装它时不断收到错误消息。

现在,我的服务中什至没有任何东西,我只想能够安装该服务。我使用 Visual C# > Windows 桌面下的 Windows 服务 (.NET Framework) 模板在 Visual Studio 2017 中创建了一个新项目。然后我通过右键单击 > 添加安装程序添加了安装程序。对于 serviceProcessInstaller1,我将帐户设置为 LocalSystem。而且……就是这样!

现在,我尝试安装它。我打开命令提示符(以管理员身份运行)然后我做

InstallUtil ServiceTest.exe

事情开始顺利,但随后我收到一条消息:

“在安装阶段发生异常。 System.UnauthorizedAccessException:试图执行 未经授权的操作。”

什么可能会生成此消息?

我用谷歌搜索并尝试了以下方法:

  • 确保我以管理员身份运行命令提示符。
  • 将完全控制权分配给 项目文件夹到我的管理员帐户。
  • 将权限配置为完全 在 regedit 的安全日志中控制管理员帐户。
  • 登录我的 计算机作为管理员(而不是普通用户)并运行它 那样。

尽管有这些事情,我还是不断收到同样的错误。我还能做错什么?非常感谢一些建议!

注意:我正在尝试在我的计算机上本地安装此服务。

编辑:根据请求,这里是日志。顺便说一下,我将我的项目命名为“FailedService”,因为我无法让它工作!

InstallUtil.InstallLog

Running a transacted installation.

Beginning the Install phase of the installation.
See the contents of the log file for the C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe assembly's progress.
The file is located at C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.InstallLog.

An exception occurred during the Install phase.
System.UnauthorizedAccessException: Attempted to perform an unauthorized operation.

The Rollback phase of the installation is beginning.
See the contents of the log file for the C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe assembly's progress.
The file is located at C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.InstallLog.

The Rollback phase completed successfully.

The transacted install has completed.

FailedService.InstallLog

Installing assembly 'C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe'.
Affected parameters are:
   logtoconsole = 
   logfile = C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.InstallLog
   assemblypath = C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe
Installing service Service1...
Creating EventLog source Service1 in log Application...
Rolling back assembly 'C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe'.
Affected parameters are:
   logtoconsole = 
   logfile = C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.InstallLog
   assemblypath = C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe
Restoring event log to previous state for source Service1.

【问题讨论】:

  • 您能否显示您的代码部分,您可以在其中覆盖 oninstallation 方法以将用户帐户设置为本地系统?试着先把它去掉,然后有用吗?
  • 我认为我没有覆盖 oninstallation 方法...要将用户帐户设置为本地系统,我在设计视图中打开 ProjectInstaller.cs,左键单击 serviceProcessInstaller1,然后在属性中在 Misc 下方的框中,我将 Account 设置为“LocalSystem”。不确定这是否重要,但我尝试了 LocalService 和 NetworkService 并收到了相同的结果。您还想看我的代码吗?我将所有内容都保留为默认设置,除了我提到的内容之外没有进行任何修改?
  • 不确定,您的服务是做什么的,简单的猜测......安装服务有一个复选框,即“允许服务与桌面交互”。 Please read this post,因为设置该标志可能是一个安全问题(可以通过编程方式设置)。
  • nilsK,我必须首先安装该服务才能检查该标志。

标签: c# windows service windows-services installutil


【解决方案1】:

在提升的提示符中输入 installUtil 和您的服务二进制文件的完整路径,可以这样做。

【讨论】:

  • 与“我以管理员身份运行命令提示符”有何/为什么不同
  • 正如 dlatikay 所说,我已经以管理员身份运行提示。另外,我从完整路径开始运行 installUtil。我没有在完整路径中运行服务二进制文件,因为我已经从该目录开始工作。但是,我确实尝试从完整路径运行两者,但仍然无法正常工作。
  • 重点不在“作为管理员”,而是在“完整路径”。这是我自己也遇到的一个众所周知的错误。但无论如何,它没有工作
【解决方案2】:

我终于能够解决这个问题。通过使用 SysInternal 的进程监视器,我能够深入了解它。我打开程序并让它记录 InstallUtil.exe 的日志。从那里,我开始搜索任何显示“拒绝访问”的日志。当我对它们进行排序时,我注意到一些有趣的事情:它试图创建一个注册表项,但它一直失败!

所以我进入注册表并创建了一个名为“ServiceTest”的键。之后,我再次运行 InstallUtil 命令,它成功了!

如果其他人遇到问题,解决方案是在以下位置创建一个以您的进程名称命名的密钥:

HKLM\System\CurrentControlSet\Services\EventLog\Application\

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-14
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 2012-07-07
    相关资源
    最近更新 更多