Debugging a Windows Service

This article is primarily focused on Windows Services in debug mode so that we can debug the service to check the flow of code. In this article, we will not learn the process of debugging a Windows Service.

Step 1

Open Visual Studio and choose the Windows Service template and provide a nice name for the application.

WindowServiceTeplmplate

Step 2

Right-click on the Service1 design form. And add an installer. If you don't have any use for that then skip this step. Installers are used when you will install your service, this will help you to run your service.

ImgInstaller

Something else you may change is the name of the service and, depending on your preferences, for that you click on the property option and do the change.

ImgProperty

Step 3

Add some dummy methods and set a breakpoint.

  1. namespace WindowsServiceDemo  
  2. {  
  3.     public partial class Service1 : ServiceBase  
  4.     {  
  5.         System.Timers.Timer Otimer = null;  
  6.         double interval = 5000;  
  7.         public Service1()  
  8.         {  
  9.             InitializeComponent();  
  10.               
  11.         }  
  12.   
  13.         private void InatilizeService()  
  14.         {  
  15.             //throw new NotImplementedException();  
  16.             Otimer = new System.Timers.Timer(interval);  
  17.             Otimer.Enabled = true;  
  18.             Otimer.AutoReset = true;  
  19.             Otimer.Elapsed += new System.Timers.ElapsedEventHandler(Otimer_Elapsed);  
  20.             Otimer.Start();  
  21.         }  
  22.   
  23.         protected override void OnStart(string[] args)  
  24.         {  
  25.             InatilizeService();  
  26.         }  
  27.   
  28.         protected override void OnStop()  
  29.         {  
  30.         }  
  31.   
  32.         void Otimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)  
  33.         {  
  34.             writeToFile();  
  35.         }  
  36.   
  37.         public void writeToFile()  
  38.         {  
  39.             //throw new NotImplementedException();  
  40.   
  41.         }  
  42.     }  
  43. }  
And we can see the breakpoint that I used in the code.

Img_Breakpoint

Step 4


Run the application. When we run the application, there is a Windows Service Start Failure alert box that will appear with the message that it couldn't hit the breakpoint.

 

WindowServiceStartFailureAlert

Step 5

Working on Debug.

There are many ways to debug our Windows Services. In this article we will see some tricks to make our application in debug mode and activate the breakpoint.

DebugCode

In  this code we are creating an object and from that object we call the associated methods. This method lets us allow debugging using a breakpoint.

DebugBreakPoint1

If you call your method in the class constructor then we can also do the debugging easily.

Debugging
 

Summary

In this article we saw the debugging process of Windows Services. I hope somehow this article helps you.


Similar Articles