Is it possible to debug the Windows services in Visual Studio?
I used code like
System.Diagnostics.Debugger.Break();
but it is giving some code error like:
I got two event error: eventID 4096 VsJITDebugger and "The service did not respond to the start or control request in a timely fashion."
I'm using the
/Console
parameter in the Visual Studio project Debug → Start Options → Command line arguments:I just added this code to my service class so I could indirectly call OnStart, similar for OnStop.
Debug a Windows Service over http (tested with VS 2015 Update 3 and .Net FW 4.6)
Firstly, you have to create a Console Project within your VS Solution(Add -> New Project -> Console Application).
Within the new project, create a class "ConsoleHost" with that code:
And this is the code for the Program.cs class:
Configurations such as connectionstrings should be copied in the App.config file of the Console project.
To sturt up the console, righ-click on Console project and click Debug -> Start new instance.
Given that
ServiceBase.OnStart
hasprotected
visibility, I went down the reflection route to achieve the debugging.Note that
Thread
is, by default, a foreground thread.return
ing fromMain
while the faux-service threads are running won't terminate the process.You can make a console application. I use this
main
function:My
ImportFileService
class is exactly the same as in my Windows service's application, except the inheritant (ServiceBase
).Either that as suggested by Lasse V. Karlsen, or set up a loop in your service that will wait for a debugger to attach. The simplest is
That way you can start the service and inside Visual Studio you choose "Attach to Process..." and attach to your service which then will resume normal exution.