【问题标题】:Getting a Service to Run Inside of an Azure Worker Role让服务在 Azure 辅助角色内部运行
【发布时间】:2014-06-10 17:45:57
【问题描述】:

我有一个 Windows 服务,我需要将它作为辅助角色迁移到 Azure。在我的 Azure 解决方案中一切正常。但是,当我上传所有内容时,只会启动 Web 角色。辅助角色实例卡在以下两种状态之间循环而从未启动。

  • 正在等待角色开始...
  • 稳定角色...

由于实例无法启动,我怀疑我的问题出在 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();
    }
}
}

【问题讨论】:

  • 为什么不对所有内容进行尝试/捕获并记录异常? (我通常这样做:blog.smarx.com/posts/printf-here-in-the-cloud.)这比阅读代码并尝试猜测要容易得多。
  • 另外,当你在计算模拟器下本地运行它时它是否工作?
  • 我刚刚将工作角色分离到一个新的解决方案中(与我的 Web 角色分开),并且收到了更具体的错误消息。当我在本地运行它时,它显示“无法从命令行或调试器启动服务。必须首先安装 Windows 服务(使用 installutil.exe),然后使用 ServerExplorer、Windows 服务管理工具或 NET START 命令启动。”
  • 哦,也许它说的是真的。这是否适用于常规控制台应用程序(暂时忘记 Windows Azure)?
  • 如果您将它安装在服务器上,它可以作为 Windows 服务使用。我不是写它的人,但我正试图将它重新用作一名工人。

标签: c# azure windows-services azure-worker-roles


【解决方案1】:

我认为您问题的根源在于您基本上无法在 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/

希望有帮助

【讨论】:

猜你喜欢
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 2015-12-22
  • 2012-07-28
  • 2013-03-14
  • 2015-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多