Search code examples
c#debuggingwindows-services

debugging window service


I want to debug window service. What should i write in main() to enable debugging in window service. I am developing window service using C#.

#if(DEBUG)
      System.Diagnostics.Debugger.Break();
      this.OnStart(null);
      System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
 #else
      ServiceBase.Run(this);
 #endif

i wrote above code segment but on line (this


Solution

  • I would do it like this:
    In your service's OnStart method add the call to Debugger.Break() at the top:

    protected override void OnStart(string[] args)
    {
        #if DEBUG
            Debugger.Break();
        #endif
    
        // ... the actual code
    }