The wyDay blog is where you find all the latest news and tips about our existing products and new products to come.
Over the last couple of months I've gotten a surprising number of questions asking about the same thing: how to debug a Windows Service like you would debug a normal Window Forms application. There are lots of hacks to pseudo-debug a Windows service like running your code from a console, or even adding a massive amount of logging throughout your code.
But what if you need to run your Windows Service as it normally runs (i.e. as an actual service) in order to track down bugs?
The good news is that's there's a little snippet of code to make debugging Windows Services a snap. The first thing to do is to add the following DebugMode()
function to your app:
C#
/// <summary>Helper function to attach a debugger to the running service.</summary>
[Conditional("DEBUG")]
static void DebugMode()
{
if (!Debugger.IsAttached)
Debugger.Launch(); Debugger.Break();
}
Visual Basic .NET
''' <summary>Helper function to attach a debugger to the running service.</summary>
<Conditional("DEBUG")>
Shared Sub DebugMode()
If Not Debugger.IsAttached Then
Debugger.Launch()
End If Debugger.Break()
End Sub
When you call the newly added DebugMode()
function within your Windows Service, if there isn't already a debugger attached to your service it gives you the option of adding a debugger:
Now you can debug your service like you would a Windows Forms application. Also, because you're using the [Conditional("DEBUG")]
attribute on the DebugMode()
function when you compile your service in "Release" mode all the calls to the "DebugMode()
" function will be removed.
Pretty cool, huh?
Subscribe to our blog's RSS Feed or follow Wyatt (CEO of wyDay) on Mastodon (@wyatt@hachyderm.io) to keep up-to-date with our latest posts.
Wow that is so useful! I've been Debug.WriteLine() forever and its a pain.
Pretty elegant solution, thank you! :)
We have used the following VS macros:
I actually followed a link over to see if you had any obvious documentation needs - bit of a waste of time, I think! This is all humblingly good stuff. I may blog about this later.
M
Thanks, Martin.
you are the man, saved me hours, thank you
For anybody wanting to use this method on Windows 8, it won't work without a registry tweak. Please check this forum post for info:
http://forums.arcgis.com/threads/69842-Debugging-your-SOE-on-Windows-8
Thanks, Adam.
Hey Wyatt... I am new to windows services. i have just created a windows service in vb.net in vs2005. now i want to debug this service and check whether its working or not. so would you please explain me the trick how to do this? and where to apply these code?
Man, I hate it when someone just posts a snippet of code and not a full fledged example.
Doug, that's all the code you need. That function (DebugMode() ) and calling the function somewhere in your service.
I'm not going to write your service for you.
If you want an example C# or VB.NET service, then google it. There are about a billion examples already.
I didn't ask you to write it. I just think it needs to show a more detailed example, thanks
Good and simple, thanks a lot for this Wyatt. I don't know why the old way stopped working, I built a lot of Windows Services on .Net 2.0 and I could simply attach my code to the service process and I could debug it from there, I guess I missed that part when moving from 2.0 to 4.0.
My guess is that fixing the error (or at least telling me what the error is) will solve your problem.