获取服务的内部的Azure工作者角色来运行 [英] Getting a Service to Run Inside of an Azure Worker Role

查看:106
本文介绍了获取服务的内部的Azure工作者角色来运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口服务,我需要迁移到Azure的上作为一个工作者角色。一切都建立在我的Azure解决方案的罚款。然而,当我上载的一切只是Web角色开始。工人角色实例得到以下两种状态之间循环卡住而没有启动。


  • 等待中的作用开始...

  • 稳定作用...

由于实例启动失败,我怀疑我的问题出在我的WorkerRole.cs code的地方。下面你会发现,code。我还包含了code在情况下,它是有关这个问题的服务。我做了什么错了?

这是我的WorkerRole.cs文件:

 使用系统;
使用System.Collections.Generic;
使用System.Diagnostics程序;
使用System.Linq的;
使用System.Net;
使用的System.Threading;
使用Microsoft.WindowsAzure;
使用Microsoft.WindowsAzure.Diagnostics;
使用Microsoft.WindowsAzure.ServiceRuntime;
使用Microsoft.WindowsAzure.StorageClient;
使用System.ServiceProcess;命名空间SBMWorker
{
公共类WorkerRole:RoleEntryPoint
{    公共覆盖无效的run()
    {
        ServiceBase [] ServicesToRun;
        ServicesToRun =新ServiceBase []
        {
            新的服务1()
        };
        ServiceBase.Run(ServicesToRun);        //Thread.Sleep(Timeout.Infinite);
    }}
}

这是我的Service1.cs code:

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Diagnostics程序;
使用System.Linq的;
使用System.ServiceProcess;
使用System.Text;
使用Lesnikowski.Mail;命名空间SBMWorker{
公共部分类服务1:ServiceBase
{
    私人System.Timers.Timer mainTimer;    公共服务1()
    {
        的InitializeComponent();
    }    保护覆盖无效的OnStart(字串[] args)
    {
        尝试
        {
            //配置计时器的时间间隔
            mainTimer =新System.Timers.Timer(foo.Framework.Configuration.SecondsToWaitBeforeCheckingForEmailsToProcess * 1000);
            //处理
            mainTimer.Elapsed + =新System.Timers.ElapsedEventHandler(mainTimer_Elapsed);
            //启动定时器。
            mainTimer.Start();
            //日志,我们才开始
            foo.Framework.Log.Add(foo.Framework.Log.Types.info,服务启动);
       }
        赶上(异常前)
        {
            尝试
            {
                foo.Framework.Log.Add(恩,真);
            }
            赶上{抛出;}            //确保抛出此因此该服务显示为停止......我们不希望这种服务只是在这里挂像
            //它的运行,但实际上只是无所事事
            扔;
        }
    }    保护覆盖无效调用OnStop()
    {
        如果(mainTimer!= NULL)
        {
            mainTimer.Stop();
            mainTimer = NULL;
        }
        //登录我们停止
        foo.Framework.Log.Add(foo.Framework.Log.Types.info,服务已停止);
    }    无效mainTimer_Elapsed(对象发件人,System.Timers.ElapsedEventArgs E)
    {
        mainTimer.Stop();        布尔运行code = TRUE;        如果(运行code)
        {
            尝试
            {
                //呼叫处理
                foo.Framework.EmailPackageUpdating.ProcessEmails();
            }
            赶上(异常前)
            {
                尝试
                {
                    //处理错误
                    foo.Framework.Log.Add(例如,假);
                }
                赶上{抛出; }
            }
        }        mainTimer.Start();
    }
}
}


解决方案

我觉得你的问题的根源是,你基本上不能以编程方式在Azure中的角色安装服务。你需要使用一个.cmd启动脚本和InstallUtil正确安装服务,那么你可以从脚本或从code启动它(觉得从剧本往往是preferred的订单中,执行的理由)。

下面是关于如何做到这一点博客文章:<一href=\"http://blogs.msdn.com/b/golive/archive/2011/02/11/installing-a-windows-service-in-a-worker-role.aspx\">http://blogs.msdn.com/b/golive/archive/2011/02/11/installing-a-windows-service-in-a-worker-role.aspx

下面是另一篇博客中,需要一个有点不同的方法(创建的.Net应用程序,执行安装,但同样为启动脚本的一部分):<一href=\"http://www.bondigeek.com/blog/2011/03/25/runninginstalling-a-windows-service-in-an-azure-web-role/\">http://www.bondigeek.com/blog/2011/03/25/runninginstalling-a-windows-service-in-an-azure-web-role/

希望帮助

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.

  • Waiting for the role to start...
  • Stabilizing role...

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?

This is my WorkerRole.cs file:

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

}
}

This is my Service1.cs 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();
    }
}
}

解决方案

I think the root of your problem is that you basically can't install a service programmatically in Azure roles. You need to use a .cmd startup script and the InstallUtil to properly install the service then you can start it from the script or from your code (think from the script is often preferred for order-of-execution reasons).

Here's a blog post on how to do it: http://blogs.msdn.com/b/golive/archive/2011/02/11/installing-a-windows-service-in-a-worker-role.aspx

Here's another blog post that takes a little different approach (creating a .Net app that executes the installation, but again as part of the startup script): http://www.bondigeek.com/blog/2011/03/25/runninginstalling-a-windows-service-in-an-azure-web-role/

Hope that helps

这篇关于获取服务的内部的Azure工作者角色来运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆