我有一個需要作爲工作者角色遷移到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();
}
}
}
你爲什麼不把一個try/catch周圍的一切,並記錄異常? (我通常這樣做:http://blog.smarx.com/posts/printf-here-in-the-cloud。)這比讀代碼和嘗試猜測要容易很多。 – smarx
另外,當你在計算模擬器下本地運行它時它是否工作? – smarx
我只是將工作者角色分離成一個新的解決方案(與我的web角色分開),並獲得更具體的錯誤消息。當我在本地運行它時,它說:「無法從命令行或調試器啓動服務。首先必須安裝Windows服務(使用installutil.exe),然後使用ServerExplorer,Windows服務管理工具或NET START命令啓動。 – hughesdan