Debugging Windows Services

I am having trouble debugging my windows services. I have it started through services and attached it as a process from the debug menu. Once I am in the debug mode I cannot step into the code using breakpoints. When I do a mouse over my breakpoints it says "The breakpoint will not currently be hit. No executable code is currently loaded at this location." Pls help. Thanks


Answer this question

Debugging Windows Services

  • jai_123

    Are you sure that there is code to be executed by the time you attach

    One problem I have seen quite often is that when trying to debug the OnStart portion of a service, it has already finished executing by the time you get inside of it. To solve this problem I will often put an extended sleep inside or have it do something trivial like start a startup thread (so that OnStart can return in time) and have a sleep followed by a breakpoint in the ThreadStart.



  • Ree84

    Please do the following steps, since debugging a Windows Service is different than a normal application.

    Debug the Windows Service

    As with all other aspects, debugging a Windows Service is different than a normal application. More steps are required to debug a Windows Service. The service cannot be debugged by simply executing it in the development environment as you can with a normal application. The service must first be installed and started, which we covered in the previous section. Once it is started you attach Visual Studio to the running process in order to step through and debug the code. Remember, each change you make to the Windows Service will require you to uninstall and reinstall the service.

    Attach to a Running Windows Service

    Here are the directions for attaching to a Windows Service in order to debug the application. These instructions assume that you have already installed the Windows Service and it is currently running.

    1. Load the project into Visual Studio
    2. Click on the Debug menu
    3. Click on the Processes menu item
    4. Make sure the Show system processes is selected
    5. Locate your process in the Available Processes list based on the name of your executable and click on it
    6. Click the Attach button
    7. Click OK
    8. Click Close
    9. Set a break point in the timer1_Elapsed method and wait for it to execute

    Summary

    You should now have a rough idea of what windows services are, how to create, install, and debug them. There is additional functionality with Windows Services that you can explore. This functionality includes the capability to pause (OnPause) and resume (OnContinue). The ability to pause and resume are not enabled by default and are setup through the Windows Service properties.

    I hope it will be useful.

    -Sneha



  • Jay McLain

    System.Diagnostics.Debugger.Break() is very useful in debugging windows services


  • Debugging Windows Services