Windows服务-安装服务(C#)时出现UnauthorizedAccessException错误

时间:2018-11-21 15:37:15

标签: c# windows service windows-services installutil

所以我尝试在Visual Studio 2017中使用C#创建服务,并且在尝试安装服务时始终出现错误。

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

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

InstallUtil ServiceTest.exe

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

  

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

这是什么产生此消息?

我已经用Google搜索并尝试了以下操作:

  • 确保以管理员身份运行命令提示符。
  • 将完全控制权分配给 项目文件夹到我的管理员帐户。
  • 将权限配置为“完全” 在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.

2 个答案:

答案 0 :(得分:0)

在提升的提示符下输入installUtil和服务二进制文件的完整路径。

答案 1 :(得分:0)

我终于可以解决此问题。我可以使用SysInternal的Process Monitor深入了解它。我打开了程序,并获取了InstallUtil.exe的日志。从那里开始,我开始搜索任何显示“访问被拒绝”的日志。当我对它们进行排序时,我注意到了一些有趣的事情:它试图创建一个注册表项,但是一直失败!

因此,我进入了注册表并创建了一个名为“ ServiceTest”的密钥。之后,我再次运行InstallUtil命令,它开始工作了!

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

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

相关问题