Convenient debugging of Windows services
Debugging Windows Service applications is not such a trivial task as it seems. The problem is that when debugging this type of application, you cannot use standard Visual Studio tools such as breakpoints and other useful tools. All due to the fact that the Windows Service application cannot be launched on F5 directly from Visual Studio. Nevertheless, MSDN offers us several ways to debug them. Most likely, many developers did not even come across them until they began to create their first service. This is using event log entries and connecting to a process . Both of these methods are well described in MSDN, but they only allow debugging of a service that is already running. Because of this, the code that launches the service itself cannot be tested in the OnStart () method.
Below I want to talk about the way you can get around this limitation, and test my service as a regular console application. And, therefore, take advantage of all that Visual Studio offers us for this.
First we need to create a new project. Let's call it TestWinService. Because this is a service, immediately add the installer to it. Right-click on an empty area of the file that just opened and select Add Installer. Now we will add a console project to our solution, from which we, in fact, will debug our service. Immediately after creating a new project, add two links to it. One on our service, TestWinService, the other on System.ServiceProcess needed to call the service.
Now the fun begins. To debug the code that starts the service, we will need to create a new thread, which will do all the necessary work. And also two methods - Start () and Stop () for its launch and completion. And, accordingly, we will call them in the methods of turning the service on and off. In the end, we get something like this. It can be seen that we added a test line and set a breakpoint on it. Now we’ll add code to the console application that will emulate starting and stopping the service. We should get something similar in the picture below. All is ready! It remains only to press F5 and make sure that the debugger has stopped on the test line.
Using this method, debugging services is no more difficult than debugging a regular console application. I hope this is useful to someone.
Below I want to talk about the way you can get around this limitation, and test my service as a regular console application. And, therefore, take advantage of all that Visual Studio offers us for this.
First we need to create a new project. Let's call it TestWinService. Because this is a service, immediately add the installer to it. Right-click on an empty area of the file that just opened and select Add Installer. Now we will add a console project to our solution, from which we, in fact, will debug our service. Immediately after creating a new project, add two links to it. One on our service, TestWinService, the other on System.ServiceProcess needed to call the service.
Now the fun begins. To debug the code that starts the service, we will need to create a new thread, which will do all the necessary work. And also two methods - Start () and Stop () for its launch and completion. And, accordingly, we will call them in the methods of turning the service on and off. In the end, we get something like this. It can be seen that we added a test line and set a breakpoint on it. Now we’ll add code to the console application that will emulate starting and stopping the service. We should get something similar in the picture below. All is ready! It remains only to press F5 and make sure that the debugger has stopped on the test line.
Using this method, debugging services is no more difficult than debugging a regular console application. I hope this is useful to someone.