<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Notes from the Field : debugging</title><link>http://blogs.msdn.com/field_notes/archive/tags/debugging/default.aspx</link><description>Tags: debugging</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>How To Debug Windows Service OnStart</title><link>http://blogs.msdn.com/field_notes/archive/2008/07/21/how-to-debug-windows-service-onstart.aspx</link><pubDate>Tue, 22 Jul 2008 04:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8762928</guid><dc:creator>jiwasz@microsoft.com</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/field_notes/comments/8762928.aspx</comments><wfw:commentRss>http://blogs.msdn.com/field_notes/commentrss.aspx?PostID=8762928</wfw:commentRss><description>&lt;P&gt;The article, &lt;A class="" title="How to: Debug Windows Service Applications" href="http://msdn.microsoft.com/en-us/library/7a50syb3(VS.80).aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/7a50syb3(VS.80).aspx"&gt;How to: Debug Windows Service Applications&lt;/A&gt;&amp;nbsp;details how to attach to a running process to debug a service. This works when the code you're debugging a logical issue rather than finding the root cause of an early failure when the service starts up. Debugging the OnStart method can be problematic.&amp;nbsp;According to the article:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;One way to work around this is to create a temporary second service in your service application that exists only to aid in debugging. You can install both services, and then start this "dummy" service to load the service process. Once the temporary service has started the process, you can then use the &lt;B&gt;Debug&lt;/B&gt; menu in Visual Studio to attach to the service process. &lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;After attaching to the process, you can set breakpoints and use these to debug your code. Once you exit the dialog box you use to attach to the process, you are effectively in debug mode. You can use the &lt;B&gt;Services Control Manager&lt;/B&gt; to start, stop, pause and continue your service, thus hitting the breakpoints you've set. You would later remove this dummy service after debugging is successful.&lt;/EM&gt; &lt;/P&gt;
&lt;P&gt;Rather than creating a new service solely for debugging purposes, you can use the System.Diagnostics.Debugger.&lt;A class="" title=Launch href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.launch.aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.launch.aspx"&gt;Launch&lt;/A&gt; method.&amp;nbsp;In order to use this approach the service must be compiled for Debug and the PDB file must be available and preferably in the same directory as the installed service. Don't forget that you don't need a MSI to install the service. Running &lt;A class="" title=installutil.exe href="http://msdn.microsoft.com/en-us/library/50614e95(VS.80).aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/50614e95(VS.80).aspx"&gt;installutil.exe&lt;/A&gt; against a DLL containing an &lt;A class="" title=Installer href="http://msdn.microsoft.com/en-us/library/system.configuration.install.installer.aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/system.configuration.install.installer.aspx"&gt;Installer&lt;/A&gt; for the service is sufficient.&lt;/P&gt;
&lt;P&gt;After you install and start the service, on Vista, you are presented with the following dialog:&lt;/P&gt;
&lt;P&gt;&lt;IMG title="Debug JIT Dialog" style="WIDTH: 616px; HEIGHT: 333px" height=333 alt="Debug JIT Dialog" src="http://blogs.msdn.com/photos/jiwasz/images/8762906/original.aspx" width=616 mce_src="http://blogs.msdn.com/photos/jiwasz/images/8762906/original.aspx"&gt;&lt;/P&gt;
&lt;P&gt;After selecting, "Yes, debug MyNewService.exe" you will be presented with a dialog asking whether or not you want to grant or deny access. Accessing services requires administrator level access. Then, you're given the option to load the IDE:&lt;/P&gt;
&lt;P&gt;&lt;IMG title="Launch IDE" style="WIDTH: 409px; HEIGHT: 441px" height=441 alt="Launch IDE" src="http://blogs.msdn.com/photos/jiwasz/images/8762904/original.aspx" width=409 mce_src="http://blogs.msdn.com/photos/jiwasz/images/8762904/original.aspx"&gt;&lt;/P&gt;
&lt;P&gt;Finally, the Visual Studio .NET IDE will load and with the breakpoint set to the line with Debugger.Launch.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;IMG title=Debugger style="WIDTH: 516px; HEIGHT: 120px" height=120 alt=Debugger src="http://blogs.msdn.com/photos/jiwasz/images/8762903/original.aspx" width=516 mce_src="http://blogs.msdn.com/photos/jiwasz/images/8762903/original.aspx"&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;Just make sure to remove this line of code before you check the service back into a source control system.&amp;nbsp;This is something that should never go into a production environment.&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;While writing this entry I also came across a useful post documenting five different ways to debug a Windows service:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;A class="" title="Five Ways to Debug a Windows Service" href="http://blogs.msdn.com/yaleeyangmsblog/archive/2007/05/02/three-ways-debugging-net-windows-service.aspx" target=_blank mce_href="http://blogs.msdn.com/yaleeyangmsblog/archive/2007/05/02/three-ways-debugging-net-windows-service.aspx"&gt;Five Ways to Debug a Windows Service&lt;/A&gt;&lt;/P&gt;
&lt;SCRIPT src="http://Services.social.microsoft.com/widgets/bookmark.jss?type=0&amp;amp;brand=Msdn&amp;amp;locale=1033" type=text/javascript&gt;Bookmark on MSDN&lt;/SCRIPT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8762928" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/field_notes/archive/tags/debugging/default.aspx">debugging</category><category domain="http://blogs.msdn.com/field_notes/archive/tags/Windows+Service/default.aspx">Windows Service</category></item></channel></rss>