How to debug a windows service using breakpoints?

后端 未结 5 1580
醉梦人生
醉梦人生 2020-12-17 04:38

I have a windows service with a timer. Its very hard to debug it. Because I start the service and put break points in different parts of the code. When I attach the process,

相关标签:
5条回答
  • 2020-12-17 04:46

    It is not as convenient as breakpoints in the debugger but I found writing output to a simple log files helps. We have services that spawn threads to perform work and writing out to a log file has helped pinpoint problems.

    0 讨论(0)
  • 2020-12-17 04:47

    Simplest approach...

    Put the business - functional portions of the service code in a separate class, in a separate library assembly... Then you can debug it independantly using a simple console app...

    0 讨论(0)
  • 2020-12-17 04:55

    I assume you're talking about .Net of course. I've always used the following code to debug my services. I place it where I want to the debugger to launch. Start the service and it automatically launches Visual Studio. Works well for me.

    System.Diagnostics.Debugger.Launch();

    System.Diagnostics.Debugger.Debug();

    0 讨论(0)
  • 2020-12-17 05:06

    You can use "Image File Execution Options" and configure that each time that the service start it would start inside a debugger.
    That debugger can be WinDBG or Visual Studio.

    0 讨论(0)
  • 2020-12-17 05:09

    Just press F5. You can run Windows Services regular just like apps.

    Since we have no other command-line arguments we just use the presense of any command-line argument as a signal to run as a normal windows app. You could also require the presence of a specific command-line argument (i.e. /debug).

    If sArgs IsNot Nothing AndAlso sArgs.Length > 0 Then
        ' If there are command-line args then run in non-service mode
        Dim svc As ServiceMain = New ServiceMain(True)
        svc.OnStart(Nothing)
        System.Windows.Forms.Application.Run()
        svc.OnStop()
    Else
        ' If no command-line args then run normally
        Dim ServicesToRun() As System.ServiceProcess.ServiceBase
        ServicesToRun = New System.ServiceProcess.ServiceBase() {New ServiceMain(False)}
        System.ServiceProcess.ServiceBase.Run(ServicesToRun)
    End If
    
    0 讨论(0)
提交回复
热议问题