<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>Nick Nguyen's Blog</title>
  <link rel="alternate" type="text/html" href="http://www.nansoftware.com/blog/" />
  <link rel="self" href="http://www.nansoftware.com/blog/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2010-01-07T13:08:03.5064348-08:00</updated>
  <author>
    <name>NaN Software</name>
  </author>
  <subtitle>Just Bing It...Just Kidding</subtitle>
  <id>http://www.nansoftware.com/blog/</id>
  <generator uri="http://dasblog.info/" version="2.3.9074.18820">DasBlog</generator>
  <entry>
    <title>Debug Windows Services Like a Console App</title>
    <link rel="alternate" type="text/html" href="http://www.nansoftware.com/blog/Debug-Windows-Services-Like-A-Console-App.aspx" />
    <id>http://www.nansoftware.com/blog/PermaLink,guid,67cbac88-0b16-466d-862d-02c6ce8296e5.aspx</id>
    <published>2010-01-04T07:02:56.4591201-08:00</published>
    <updated>2010-01-07T13:08:03.5064348-08:00</updated>
    <category term="code" label="code" scheme="http://www.nansoftware.com/blog/CategoryView,category,code.aspx" />
    <author>
      <name>Nick Nguyen</name>
    </author>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
 
</p>
        <p>
Historically, debugging windows services can be a minor pain. One either has to either
emulate the service controller behavior using a separate console application project
or install the service, start it, set a break point and attach the debugger.
</p>
        <p>
          <strong>
          </strong> 
</p>
        <p>
          <strong>
            <font color="#0080ff" size="3">Mimicking a console application</font>
          </strong>
        </p>
        <p>
          <strong>
          </strong> 
</p>
        <p>
          <strong>Listing 1: ServiceBase extension method</strong>
        </p>
        <pre class="brush: csharp;">        /// &lt;summary&gt;
        /// Runs the service.
        /// &lt;/summary&gt;
        /// &lt;param name="serviceBase"&gt;The service base.&lt;/param&gt;
        /// &lt;param name="args"&gt;The args.&lt;/param&gt;
        public static void RunService(this ServiceBase serviceBase, string[] args)
        {
            if (Debugger.IsAttached)
            {
                using (var autoResetEvent = new AutoResetEvent(false))
                {
                    const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.IgnoreCase |
                                               BindingFlags.Instance;

                    var methodInfo = serviceBase.GetType().GetMethod("OnStart", flags);
                    if (methodInfo != null)
                        methodInfo.Invoke(serviceBase, new object[] { args });

                    autoResetEvent.WaitOne();
                }
            }
            else
            {
                ServiceBase.Run(new[] { serviceBase });
            }
        }</pre>
        <p>
Pretty simple stuff…check that the debugger is attached…use reflection to invoke the
protected ‘OnStart’ method and block the main thread infinitely with an ‘AutoResetEvent';
otherwise, run the service as normal. Here’s the usage: 
</p>
        <pre class="brush: csharp;">    internal static class Program
    {
        /// &lt;summary&gt;
        /// The main entry point for the application.
        /// &lt;/summary&gt;
        private static void Main()
        {
//            ServiceBase[] ServicesToRun;
//            ServicesToRun = new ServiceBase[] 
//            { 
//                new Service1() 
//            };
//            ServiceBase.Run(ServicesToRun);

            new Service1().RunService(null);
        }
    }</pre>
        <p>
  
</p>
        <p>
          <strong>Listing 3: Key service controller event handlers</strong>
        </p>
        <pre class="brush: csharp;">private void OnServiceTimerElapsed(object sender, ElapsedEventArgs e)
        {
            _serviceTimer.Stop();

            var dateTime = DateTime.Now;
            Debug.WriteLine(string.Format("Timer elapsed at {0} {1}...", dateTime.ToShortDateString(), dateTime.ToLongTimeString()));

            _serviceTimer.Start();
        }

        protected override void OnStart(string[] args)
        {
            _serviceTimer.AutoReset = true;
            _serviceTimer.Enabled = true;
            _serviceTimer.Start();

            Debug.WriteLine("Service started...");
        }</pre>
        <p>
 
</p>
        <p>
To see this in action hit F5 or run the service project instance and you can see that
the service is running based on the timer elapsed event in Listing 3. <a href="http://www.nansoftware.com/blog/content/binary/WindowsLiveWriter/DebuggingaWindowsServiceLikeaConsoleApp_A496/image_6.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="service debug tracing" border="0" alt="service debug tracing" align="left" src="http://www.nansoftware.com/blog/content/binary/WindowsLiveWriter/DebuggingaWindowsServiceLikeaConsoleApp_A496/image_thumb_2.png" width="484" height="375" /></a></p>
        <img width="0" height="0" src="http://www.nansoftware.com/blog/aggbug.ashx?id=67cbac88-0b16-466d-862d-02c6ce8296e5" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Congratulations, you've installed dasBlog with Web Deploy!</title>
    <link rel="alternate" type="text/html" href="http://www.nansoftware.com/blog/Congratulations-Youve-Installed-DasBlog-With-Web-Deploy.aspx" />
    <id>http://www.nansoftware.com/blog/PermaLink,guid,b705c37b-b47f-4e8d-8f8b-091efc4cb684.aspx</id>
    <published>2009-03-11T00:00:00-07:00</published>
    <updated>2009-03-11T23:10:18.9161264-07:00</updated>
    <category term="dasBlog" label="dasBlog" scheme="http://www.nansoftware.com/blog/CategoryView,category,dasBlog.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
After <a href="Login.aspx">logging in</a>, be sure to visit all the options under <a href="EditConfig.aspx">Configuration</a> in
the Admin Menu Bar above. There are <a href="http://dasblog.info/ThemeScreenShots.aspx">26
themes to choose from</a>, and you can also <a href="http://dasblog.info/ThemesAndMacros.aspx">create
your own</a>.
</p>
        <p>
        </p>
        <img width="0" height="0" src="http://www.nansoftware.com/blog/aggbug.ashx?id=b705c37b-b47f-4e8d-8f8b-091efc4cb684" />
      </div>
    </content>
  </entry>
</feed>