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.
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.
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.
Step 3
Add some dummy methods and set a breakpoint.
- namespace WindowsServiceDemo
- {
- public partial class Service1 : ServiceBase
- {
- System.Timers.Timer Otimer = null;
- double interval = 5000;
- public Service1()
- {
- InitializeComponent();
-
- }
-
- private void InatilizeService()
- {
-
- Otimer = new System.Timers.Timer(interval);
- Otimer.Enabled = true;
- Otimer.AutoReset = true;
- Otimer.Elapsed += new System.Timers.ElapsedEventHandler(Otimer_Elapsed);
- Otimer.Start();
- }
-
- protected override void OnStart(string[] args)
- {
- InatilizeService();
- }
-
- protected override void OnStop()
- {
- }
-
- void Otimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- writeToFile();
- }
-
- public void writeToFile()
- {
-
-
- }
- }
- }
And we can see the breakpoint that I used in the code.
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.
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.
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.
If you call your method in the class constructor then we can also do the debugging easily.
Summary
In this article we saw the debugging process of Windows Services. I hope somehow this article helps you.