title: Getting a Service to Run Inside of an Azure Worker Role
body: <p>I have a windows service that I need to migrate to onto Azure as a Worker Role.  Everything builds fine in my Azure solution.  However, when I upload everything only the web role starts.  The worker role instance gets stuck cycling between the following two statuses without ever starting.</p>
  
  <ul>
  <li>Waiting for the role to start...</li>
  <li>Stabilizing role...</li>
  </ul>
  
  <p>Since the instance is failing to start I suspect my problem lies somewhere in my WorkerRole.cs code.  Below you'll find that code.  I've also included the code for the service in case it's relevant to the question.  What did I do wrong?</p>
  
  <p>This is my WorkerRole.cs file:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Net;
  using System.Threading;
  using Microsoft.WindowsAzure;
  using Microsoft.WindowsAzure.Diagnostics;
  using Microsoft.WindowsAzure.ServiceRuntime;
  using Microsoft.WindowsAzure.StorageClient;
  using System.ServiceProcess;
  
  namespace SBMWorker
  {
  public class WorkerRole : RoleEntryPoint
  {
  
      public override void Run()
      {
          ServiceBase[] ServicesToRun;
          ServicesToRun = new ServiceBase[] 
          { 
              new Service1() 
          };
          ServiceBase.Run(ServicesToRun);
  
          //Thread.Sleep(Timeout.Infinite);
      }
  
  }
  }
  </code></pre>
  
  <p>This is my Service1.cs code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Diagnostics;
  using System.Linq;
  using System.ServiceProcess;
  using System.Text;
  using Lesnikowski.Mail;
  
  namespace SBMWorker
  
  {
  public partial class Service1 : ServiceBase
  {
      private System.Timers.Timer mainTimer;
  
      public Service1()
      {
          InitializeComponent();
      }
  
      protected override void OnStart(string[] args)
      {
          try
          {
              // config the timer interval
              mainTimer = new System.Timers.Timer(foo.Framework.Configuration.SecondsToWaitBeforeCheckingForEmailsToProcess * 1000);
              // handling
              mainTimer.Elapsed += new System.Timers.ElapsedEventHandler(mainTimer_Elapsed);
              // startup the timer.  
              mainTimer.Start();
              // log that we started
              foo.Framework.Log.Add(foo.Framework.Log.Types.info, "SERVICE STARTED");
         }
          catch (Exception ex)
          {
              try
              {
                  foo.Framework.Log.Add(ex, true);
              }
              catch{throw;}
  
              // make sure the throw this so the service show as stopped ... we dont want this service just hanging here like
              // its running, but really just doing nothing at all
              throw;
          }
      }
  
      protected override void OnStop()
      {
          if (mainTimer != null)
          {
              mainTimer.Stop();
              mainTimer = null;
          }
          // log that we stopped
          foo.Framework.Log.Add(foo.Framework.Log.Types.info, "SERVICE STOPPED"); 
      }
  
      void mainTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
      {
          mainTimer.Stop();
  
          bool runCode = true;
  
          if (runCode)
          {
              try
              {
                  // call processing
                  foo.Framework.EmailPackageUpdating.ProcessEmails();
              }
              catch(Exception ex)
              {
                  try
                  {
                      // handle error 
                      foo.Framework.Log.Add(ex, false);
                  }
                  catch { throw; }
              }
          }
  
          mainTimer.Start();
      }
  }
  }
  </code></pre>