10

ワーカーロールとしてAzureに移行する必要があるWindowsサービスがあります。私のAzureソリューションではすべてが正常に構築されています。ただし、すべてをアップロードすると、Webロールのみが開始されます。ワーカーロールインスタンスは、起動せずに次の2つのステータス間を循環してスタックします。

  • 役割が開始するのを待っています...
  • 安定化の役割...

インスタンスの起動に失敗しているため、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();
    }
}
}
4

1 に答える 1

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 /

お役に立てば幸い

于 2011-08-14T20:10:09.380 に答える