Compartilhar via


Como: Depurar aplicativos de serviço do Windows

Because a service must be run from within the context of the Services Control Manager rather than from within Visual Studio, debugging a service is not as straightforward as debugging other Visual Studio application types. To debug a service, you must start the service and then attach a debugger to the process in which it is running. You can then debug your application using all of the standard debugging functionality of Visual Studio.

Aviso

You should not attach to a process unless you know what the process is and understand the consequences of attaching to and possibly killing that process. For example, if you attach to the WinLogon process and then stop debugging, the system will halt because it cannot operate without WinLogon.

You can only attach the debugger to a running service. The attachment process interrupts the current functioning of your service; it does not actually stop or pause the service's processing. That is, if your service is running when you begin debugging, it is still technically in the Started state as you debug it, but its processing has been suspended.

Anexar ao processo do serviço permite que você depurar a maior parte, mas não todos o código do serviço; Por exemplo, porque o serviço já foi iniciado, você não pode depurar o código no serviço de OnStart método dessa forma, ou o código de Main método que é usado para carregar o serviço. One way to work around this is to create a temporary second service in your service application that exists only to aid in debugging. You can install both services, and then start this "dummy" service to load the service process. Once the temporary service has started the process, you can then use the Debug menu in Visual Studio to attach to the service process.

After attaching to the process, you can set breakpoints and use these to debug your code. Once you exit the dialog box you use to attach to the process, you are effectively in debug mode. You can use the Services Control Manager to start, stop, pause and continue your service, thus hitting the breakpoints you've set. You would later remove this dummy service after debugging is successful.

ObservaçãoObservação

Depuração de OnStart método pode ser difícil porque o Gerenciador de serviços Windows impõe um limite de 30 segundos em todas as tentativas de iniciar um serviço. For more information, see Solução de problemas: Depuração de serviços do Windows.

To debug a service

  1. Install your service. For more information, see Como: Instalar e desinstalar serviços.

  2. Start your service, either from Services Control Manager, Server Explorer, or from code. For more information, see Como: Iniciar serviços.

  3. In Visual Studio, choose Processes from the Debug menu.

    The Processes dialog box appears.

  4. Click Show system processes.

  5. In the Available Processes section, click the process for your service, and then click Attach.

    Dica

    The process will have the same name as the executable file for your service.

    The Attach to Process dialog box appears.

  6. Choose any appropriate options, and then click OK to close the dialog box.

    ObservaçãoObservação

    You are now in debug mode.

  7. Set any breakpoints you want to use in your code.

  8. Access the Services Control Manager and manipulate your service, sending stop, pause, and continue commands to hit your breakpoints. Para obter mais informações sobre a execução de O Gerenciador de controle de serviços, consulte Como: Iniciar serviços.

Consulte também

Tarefas

Como: Instalar e desinstalar serviços

Como: Iniciar serviços

Conceitos

Introdução aos Aplicativos de Serviço do Windows