【发布时间】: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