3 Ways To Debug A Web Service In Visual Studio

Debugging a windows service is not that straightforward a task as debugging other web or windows application are in Visual Studio (by just pressing F5). Instead, we need to do some tricky tasks to debug it as the service runs under the context of the Service Control Manager. In my research I found 3 different ways to debug a windows service.

1. Attaching the debugger to the services’s process ( Need to INSTALL the service)

This is a commonly used method.
Steps to use this method:

— First of all we need to install the service. — From Service Explorer start the service. — Then come back to the project in Visual Studio.

— Go to the Debug menu, select Attach to Process and check Show processes from all users and Show processes in all sessions then from the Available Processes pick the process of that windows service project. [ Note: Make sure you attach the right process, attempting to debug an essential system process may destabilize your system or crash it]

Eg: If your service name is ‘ServiceDemo’ then you can find a process named ServiceDemo.exe, select and attach it.
— Give a breakpoint where you want to use in the code. Then wait till the breakpoint is hit for if any timer is specified in the service or else you can restart the service again.

This method is a bit time taking as to install the service, again attach the process just to debug it. Even sometimes it doesn’t work or shows error and we need to restart the system.
2. Using ‘Debugger.Launch() / Debugger.Break()’ methods ( Need to INSTALL the service)
Here you don’t need to attach any process rather you can call the Debugger.Launch() or Debugger.Break() method in the OnStart() function.

Eg:

protected override void OnStart(string[] args) 
{

#if (DEBUG) // Just to check if its in DEBUG mode or not. As this method works in Release mode also for .NET 2.0

Debugger.Launch(); // Attaches a debugger to the process. (We can use Debugger.Break() also)

#endif
CallBusinessLogic(); // Method containing the Business Logic of the service.

}

— Install the service and start it. During its start the Visual Studio Just-In-Time Debbuger window appears where you need to select the instance of the Visual Studio that has the Windows Service project open. Then the control goes to that debugger instance and points at the ‘ Debugger.Launch();’ line. After that you can use F11 to debug.
3. Calling the OnStart() method from another Main() function ( DO NOT need to INSTALL the service)

Saving the best for last!
In this method you don’t need to install the service. Just use the technique given below.

— Add a new class to your project. For eg : DebugService.cs
— Then add the following code in that class:

class DebugService 
{ 
static void Main() 
{

#if DEBUG

ServiceDemo obj = new ServiceDemo(); // Create object of your Service's class. 
obj.OnStart(); // Call the public OnStart() method, as mentioned in the next step. 
System.Threading. Thread.Sleep(System.Threading.Timeout.Infinite);

#endif

}

}

-- Now get back to your windows service class. Then add a public OnStart() method which will call the protected OnStart() method..

public void OnStart() // This is another Onstart method which is public in nature. 
{ 
this.OnStart(null); // Here it calls the protected OnStart() method of your service. 
}

— Right click on your project in the Solution Explorer then go to the Properties of it, there you can find the ‘Startup Object’ field. Now set the Startup Object as DebugService ( the new class where you wrote the Main() function)
— Set a breakpoint and start debugging directly from Visual Studio by pressing F5.

Note: The OnStart method has a time limit of 30 seconds on attempts to start a service.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!