{"id":41,"date":"2017-03-03T18:08:00","date_gmt":"2017-03-04T00:08:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=41"},"modified":"2017-03-03T18:08:00","modified_gmt":"2017-03-04T00:08:00","slug":"net-debugging-a-windows-service","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=41","title":{"rendered":".NET:  Debugging A Windows Service"},"content":{"rendered":"<p>1. Navigate to the windows service project.<br \/>\n2. From toolbar, select Project &#8211;&gt; Properties<br \/>\n3. Set &#8220;Output type:&#8221; dropdown to &#8220;Console Application&#8221;<br \/>\n4. Open Program.cs, locate Main method, and add the following:<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp;static void Main(string[] args)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/to test and debug<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Environment.UserInteractive)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; YourWinService serviceHb = new YourWinService(args);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; serviceHb.TestStartupAndStop(args);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var servicesToRun = new ServiceBase[]<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new YourWinService(args)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServiceBase.Run(servicesToRun);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; }<\/p>\n<p>5. Navigate to your service code and add the following method:<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;internal void TestStartupAndStop(string[] args)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.OnStart(args);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.OnStop();<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; }<\/p>\n<p>6. Set breakpoints and run as console app.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Navigate to the windows service project. 2. From toolbar, select Project &#8211;&gt; Properties 3. Set &#8220;Output type:&#8221; dropdown to &#8220;Console Application&#8221; 4. Open Program.cs, locate Main method, and add the following: &nbsp; &nbsp; &nbsp; &nbsp;static void Main(string[] args) &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/to test and debug &nbsp; &hellip; <a href=\"https:\/\/www.chrystalsander.com\/blog\/?p=41\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;.NET:  Debugging A Windows Service&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/41"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=41"}],"version-history":[{"count":0,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}