如何安装以管理员身份运行的Windows服务? [英] How do I install a windows service that runs as an administrator?
问题描述
我编写了一个安装程序,用于安装Windows服务(A),该服务需要启动/停止其他服务(B).但是,当A尝试启动/停止B时,出现此异常:
I've written an installer that installs a windows service (A) that needs to start/stop another service (B). However, when A tries to start/stop B, I get this exception:
System.InvalidOperationException:无法在计算机."上打开MyService服务. ---> System.ComponentModel.Win32Exception:访问被拒绝
System.InvalidOperationException: Cannot open MyService service on computer '.'. ---> System.ComponentModel.Win32Exception: Access is denied
安装程序将服务安装为本地服务,并通过我授予的UAC弹出窗口请求管理员权限.我还向服务中添加了一个app.manifest文件,该文件设置为要求管理员权限:
The installer installs the service as a local service, and it requests admin rights via the UAC pop-up, which I grant. I've also added an app.manifest file to the service that is set to ask for admin rights:
但是我仍然遇到该错误.
Yet I'm still getting that error.
这是我启动服务的方式(停止是一样的,除了它当然叫停止):
This is how I start the service (stopping is the same, except it calls Stop, of course):
using (Mutex mutex = new Mutex(false, "MyServiceLock"))
{
mutex.WaitOne();
if (ServiceExists(serviceName) == true)
{
using (ServiceController serviceController = new ServiceController(serviceName, "."))
{
serviceController.Start(); // this line throws the exception
}
}
mutex.ReleaseMutex();
}
为什么拒绝访问该服务?
Why might access to this service be denied?
推荐答案
服务无法请求UAC提升.在我看来,您描述的UAC提示实际上是安装程序而非服务所要求的.服务通常已经使用非常特权的帐户运行,默认情况下为LocalSystem.确保将服务配置为使用这样的特权帐户,而不是受限用户帐户.
A service cannot ask for a UAC elevation. It sounds to me that the UAC prompt you describe is actually requested by the installer, not the service. Services normally run with a very privileged account already, LocalSystem by default. Do make sure that you configure the service to use such a privileged account, not a restricted user account.
这篇关于如何安装以管理员身份运行的Windows服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!