[英]Windows Service - UnauthorizedAccessException Error when Installing Service (C#)
因此,我试图在Visual Studio 2017中使用C#创建服务,并且在尝试安装服务时始终出现错误。
现在,我的服务中什至没有任何东西,我只想能够安装该服务。 我使用Visual C#> Windows桌面下的Windows服务(.NET Framework)模板在Visual Studio 2017中创建了一个新项目。 然后,我通过右键单击>添加安装程序添加了安装程序。 对于serviceProcessInstaller1,我将帐户设置为LocalSystem。 而且...就是这样!
现在,我尝试安装它。 我打开命令提示符(以管理员身份运行),然后执行
InstallUtil ServiceTest.exe
事情开始顺利,但随后我收到一条消息:
“在安装阶段发生了异常。System.UnauthorizedAccessException:尝试执行未经授权的操作。”
什么会产生此消息?
我已经用Google搜索并尝试了以下操作:
尽管有这些事情,我仍然遇到相同的错误。 我还能做错什么? 真的很感谢一些建议!
注意:我正在尝试在计算机上本地安装此服务。
编辑:根据请求,这是日志。 顺便说一下,我将项目命名为“ 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.
在提升的提示符下输入installUtil和服务二进制文件的完整路径,可以这样做。
我终于可以解决这个问题。 我可以使用SysInternal的Process Monitor深入了解它。 我打开了程序,并获取了InstallUtil.exe的日志。 从那里开始,我开始搜索任何显示“访问被拒绝”的日志。 当我对它们进行排序时,我注意到了一些有趣的事情:它试图创建一个注册表项,但是它一直失败!
因此,我进入了注册表并创建了一个名为“ ServiceTest”的密钥。 在那之后,我再次运行InstallUtil命令,它成功了!
如果其他任何人遇到麻烦,解决方案是在以下位置创建一个以您的进程名称命名的密钥:
HKLM \\系统\\ CurrentControlSet \\服务\\事件日志\\应用程序\\
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.