系统错误 5 启动 .NET 服务时拒绝访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/574411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:11:13  来源:igfitidea点击:

System error 5 Access is denied when starting a .NET service

.netwindowsvisual-studioerror-handlingwindows-services

提问by Daniel O

When I try to start a service I created in Visual Studio I receive the following error:

当我尝试启动在 Visual Studio 中创建的服务时,收到以下错误:

System error 5 has occurred.

Access is denied.

I am running the command line with elevated privileges, so it's not that problem. Is there any place I can look to see what error is occuring.

我正在以提升的权限运行命令行,所以这不是那个问题。有什么地方可以查看发生了什么错误。

采纳答案by Daniel O

To get it to work I needed to add permissions to the output bin\debug folder for my service project.

为了让它工作,我需要为我的服务项目的输出 bin\debug 文件夹添加权限。

The Local Service account didn't have permissions to the output .exe file, and this was why the error was occuring.

本地服务帐户没有输出 .exe 文件的权限,这就是发生错误的原因。

回答by Alexander G

Had the same issue.

有同样的问题。

Fixed by running the service under "Local System Account"

通过在“本地系统帐户”下运行服务来修复

enter image description here

在此处输入图片说明

回答by hfrmobile

In my case the solution was even that simple: Run Command Prompt as administrator.

就我而言,解决方案甚至就那么简单:以管理员身份运行命令提示符。

回答by Marc Gravell

I see you've fixed the problem; but in reality, you shouldn't normally be running the service from a project's bin folder anyway - the files should be put somewhere project and profile independent (for example, under program files). For debugging purposes (when it willbe in the bin folder), you can detect whether it is a service in Main(), and if it is being run interactively just run the service code directly, rather than the usual service-start setup.

我看到你已经解决了这个问题;但实际上,您通常不应该从项目的 bin 文件夹中运行服务 - 文件应该放在与项目和配置文件无关的某个地方(例如,在程序文件下)。为了进行调试(当它在bin文件夹),你可以检测它是否是在服务Main(),而如果正在运行交互只需直接运行该服务代码,而不是通常的服务启动安装程序。

You can detect either by adding a command line argument, or you can trychecking Environment.UserInteractive.

您可以通过添加命令行参数来检测,也可以尝试检查Environment.UserInteractive.

回答by ihayet

The Local Services account doesn't seem to be privileged to control a service. So, in the service's LogOn Property, change the account type to Local System and allow service to interact with desktop.

本地服务帐户似乎没有控制服务的特权。因此,在服务的登录属性中,将帐户类型更改为本地系统并允许服务与桌面交互。

Also, make sure that, you install the service using instalutil, as an administrator.

此外,请确保您以管理员身份使用 instalutil 安装服务。

Lastly, when you want to run a service from the command prompt using the "net start [service name]" command, you have to run the command prompt as an administrator.

最后,当您想使用“net start [service name]”命令从命令提示符运行服务时,您必须以管理员身份运行命令提示符。

回答by kjellander

I had the same problem because my project and its source code was in a folder that had NTFS's Encrypting File System (EFS) enabled. This caused by compiled assemblies being encrypted aswell and the user running my service didn't have permissions to decrypt them. Removing EFS was the easy solution for this. It can be done by command line using CIPHER.EXE, which is a Windows tool.

我遇到了同样的问题,因为我的项目及其源代码位于启用了 NTFS 加密文件系统 ( EFS)的文件夹中。这是由于编译的程序集也被加密了,并且运行我的服务的用户没有解密它们的权限。删除 EFS 是解决此问题的简单方法。它可以通过命令行使用 CIPHER.EXE 来完成,CIPHER.EXE 是一个 Windows 工具。

回答by Matt Averill

Run it from Task Scheduler with highest privileges and it will work.

从具有最高权限的任务计划程序运行它,它将工作。

回答by Richard Roloff

A user account with administrator rights will prompt “are you sure?” in situations where the administrator account is not prompted. I had this problem with net stop netprofm.

具有管理员权限的用户帐户会提示“您确定吗?” 在不提示管理员帐户的情况下。我在使用 net stop netprofm 时遇到了这个问题。

To remove the prompt do this.

要删除提示,请执行此操作。

Control Panel, User Accounts, Change User Account Control settings, never notify

控制面板,用户帐户,更改用户帐户控制设置,从不通知

This seems to provide the user account with admin rights the same behavior as a the administrator account.

这似乎为具有管理员权限的用户帐户提供了与管理员帐户相同的行为。

回答by Norman Bentley

Just ran into this issue after I had run an 'sc config' to change binPath of the service.

在我运行“sc config”来更改服务的 binPath 后,刚遇到这个问题。

The only fix that worked for me was to 'sc delete' the service and install again.

唯一对我有用的修复方法是“sc 删除”服务并重新安装。

Things worked perfectly after that.

之后的事情就完美了。

回答by Enrique

I had the same problem when I migrated a service from vs05 to vs2010, from framework 2.0 till framework 4.0 at the same time. I got Access denied. As soon as a change back to framework 2.0 it worked again. The ?%¤#%&%& problem was that the initializing string for the service was incorrect (?!). The string expected quotes at the beginning and at the end!

当我将服务从 vs05 迁移到 vs2010,同时从框架 2.0 迁移到框架 4.0 时,我遇到了同样的问题。我拒绝访问。一旦更改回框架 2.0,它就会再次运行。?%¤#%&%& 问题是服务的初始化字符串不正确(?!)。字符串在开头和结尾处需要引号!

Before....path + service name" "/parameter=1 ' this had worked with framework 2.0

之前....path + service name" "/parameter=1 ' 这已经与框架 2.0 一起使用了

After...."path + service name" "/parameter=1"

之后......“路径+服务名称”“/参数=1”

Access Denied has nothing to do with the problem. Why not "Path not found " or "missing parameter"

拒绝访问与问题无关。为什么不是“找不到路径”或“缺少参数”