Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 373
Denish Avatar asked Dec 04 '25 18:12

Denish


1 Answers

I personally use this method to debug a Windows service:

static void Main() {

    if (!Environment.UserInteractive) {
        // We are not in debug mode, startup as service

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new MyServer() };
        ServiceBase.Run(ServicesToRun);
    } else {
        // We are in debug mode, startup as application

        MyServer service = new MyServer();
        service.StartService();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }
}

And create a new method in your MyServer class that will use the OnStart event:

public void StartService() {
    this.OnStart(new string[0]);
}
like image 134
Otiel Avatar answered Dec 07 '25 14:12

Otiel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!