c# - 让服务在 Azure 辅助角色内运行

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

我有一个 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();
    }
}
}

最佳答案

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

希望有帮助

关于c# - 让服务在 Azure 辅助角色内运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7032934/

相关文章:

azure - 使用移动应用程序中的 AAD 自定义登录对 Azure 应用程序服务进行身份验证

perl - 如何在 Windows 7 64 位上从 PERL 脚本创建 Windows 服务

c# - 如何在 C# Windows 服务应用程序中使用 PRISM?

c# - 在 C# 中,如何单向序列化不可序列化的对象?

c# - Excel VSTO 加载项部署错误

c# - 按钮控件中的嵌入代码 (<%= %>)

c# - 如何在 VS 中运行和测试 Windows 服务

c# - 无法使用 C# 中的参数化查询在 MS Access 中插入数据

azure - CosmosDB + 分组依据

azure - 无法在 CreateQuery 上获取 Azure TableEntity etag