Debug Windows Service

后端 未结 11 1411
既然无缘
既然无缘 2020-11-29 17:09

Scenario

I\'ve got a windows service written in C#. I\'ve read all the google threads on how to debug it, but I still can\'t get it to work. I\'ve run \"PathTo.Net

11条回答
  •  再見小時候
    2020-11-29 18:07

    Assumptions:

    1) You have the source code available in a Solution in the VS2008 IDE

    How I Debug C# Services:

    1. Install the Service using InstallUtil. (You seem like you've already done that)
    2. (If Needed) Change the Service path to the MyService.exe that is produced in your Solution's bin folder
    3. Put something like the following at the beginning of your Service's OnStart() method:

      while(true)
      {
         System.Threading.Thread.Sleep(500);
      }
      
    4. Put a breakpoint on System.Threading.Thread.Sleep(500)

    5. Build the Solution

    6. Start your Service using the Windows Service Utility

    7. While your Service is starting, in VS2008 goto Debug -> Attach To Processes...

    8. Make sure Show Processes From All Users and Show Processes In All Sessions are checked

    9. Find your MyService.exe in the list, and click Attach

    10. You should now be at the breakpoint you inserted in the infinite loop

    11. Drag the Control (Yellow Arrow) just outside the infinite loop

    12. Debug away!

    Disclaimer:

    Remember to remove the infinite loop when you want to release a build, or when you simply want to run the service normally.

提交回复
热议问题