2011-08-11 112 views
10

我有一個需要作爲工作者角色遷移到Azure的Windows服務。在我的Azure解決方案中,一切都很好。但是,當我上傳所有內容時,只有網絡角色開始。工作者角色實例在以下兩種狀態之間停滯不前,無法啓動。獲取服務以在Azure工作者角色內部運行

  • 等待角色開始...
  • 穩定作用...

由於實例啓動失敗,我懷疑我的問題在於我WorkerRole.cs代碼的某個地方。下面你會發現這個代碼。我還包含了與該問題相關的服務代碼。我做錯了什麼?

這是我WorkerRole.cs文件:

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); 
    } 

} 
} 

這是我的Service1.cs代碼:

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(); 
    } 
} 
} 
+0

你爲什麼不把一個try/catch周圍的一切,並記錄異常? (我通常這樣做:http://blog.smarx.com/posts/printf-here-in-the-cloud。)這比讀代碼和嘗試猜測要容易很多。 – smarx

+0

另外,當你在計算模擬器下本地運行它時它是否工作? – smarx

+0

我只是將工作者角色分離成一個新的解決方案(與我的web角色分開),並獲得更具體的錯誤消息。當我在本地運行它時,它說:「無法從命令行或調試器啓動服務。首先必須安裝Windows服務(使用installutil.exe),然後使用ServerExplorer,Windows服務管理工具或NET START命令啓動。 – hughesdan

回答

6

我覺得你的問題的根源是,你根本無法安裝以編程方式在Azure角色中提供服務。您需要使用.cmd啓動腳本和InstallUtil來正確安裝該服務,然後才能從腳本或代碼中啓動它(認爲從腳本開始往往是爲了執行次序的原因)。

下面是關於如何做到這一點博客文章:http://blogs.msdn.com/b/golive/archive/2011/02/11/installing-a-windows-service-in-a-worker-role.aspx

這裏的另一篇博客是需要一點點不同的方法(創建一個.net應用程序,執行安裝,但同樣爲啓動腳本的一部分): http://www.bondigeek.com/blog/2011/03/25/runninginstalling-a-windows-service-in-an-azure-web-role/

希望幫助

相關問題