0

I am working on a Windows service, and I would like to run the debugger and make breakpoints for the OnStart() method.

I have already add the line System.Diagnostics.Debugger.Launch(); in the OnStart method and breakpoints work perfectly for other methods like OnStop or the handler of the timer I set.

I tried to make breakpoints in my Onstart method and it seems like they are ignored, but the code I wrote works fine. To avoid this problem I wrote a log file for each breakpoints but I really want to make breakpoints as same as other situation.

This is my OnStart method (example):

  protected override void OnStart(string[] args)
    {
        System.Diagnostics.Debugger.Launch();

        WriteToFile("Service is started at " + DateTime.Now);  // here I make breakpoint for test but it doesn't work
        Timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
        Timer.Interval = 20000; //number in milisecinds  
        Timer.Enabled = true;
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Yamine Klioui
  • 55
  • 2
  • 9
  • 1
    Personally I debug windows services by adding an #if DEBUG block that contains a public method PublicOnStart that calls OnStart, and another #if DEBUG block in the Program.cs with a static void main that makes a new service instance and calls PublicOnStart(), then the whole lot works just by pressing play in vs – Caius Jard Apr 21 '20 at 14:03
  • Take a look at https://stackoverflow.com/questions/125964/easier-way-to-debug-a-windows-service for some alternative methods – Caius Jard Apr 21 '20 at 15:33
  • (The community wiki answer originally from Jason Miller is a version of what I mentioned) – Caius Jard Apr 21 '20 at 15:57

1 Answers1

0

I used TopShelf library , it does what i needs : debug application like a console and release it as a service.

Yamine Klioui
  • 55
  • 2
  • 9