Pregunta

He escrito un instalador que instala un servicio de Windows (A) que las necesidades para iniciar / detener otro servicio (B). Sin embargo, cuando se trata de un arranque / parada B, consigo esta excepción:

System.InvalidOperationException: No se puede MiServicio abierta en el ordenador ''. ---> System.ComponentModel.Win32Exception: Acceso denegado

El instalador instala el servicio como un servicio local y solicita derechos de administrador a través de la UAC pop-up, que concedo. También he añadido un archivo app.manifest al servicio que se establece para pedir derechos de administrador:

Sin embargo, todavía estoy consiguiendo ese error.

Esta es la forma de empezar el servicio (parada es la misma, excepto que llama Stop, por supuesto):

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();
}

¿Por qué se niega el acceso fuerza a este servicio?

¿Fue útil?

Solución

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.

Otros consejos

As a quick test, if you open up services.msc and check your server to "run as" and enter your credentials, does the error go away? It may be that the LocalService does not have access to stop other services. Providing the UAC prompt permission is likely only allowing you to install the service in the first place, not telling it to run as administrator.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top