2

Windows Azure ツール (2012 年 10 月) と共に Visual Studio 2012 を使用しています。Windows Azure クラウドのインスタンスに対していくつかのことを行う WebService があります。これはうまくいきます。

ここで、特定の間隔 (2 秒) でプロセッサの合計使用量をテーブル ストレージに格納するパフォーマンス カウンターを追加したいと考えています。したがって、WebRole onStart() メソッドにパフォーマンス カウンター メソッドを次のように新しいスレッドに追加します。

counter = new MyPerformanceCounter();
Thread thread = new Thread(new ThreadStart(counter.run));
thread.Start();
Trace.WriteLine("Thread Started");

return base.OnStart(); 

私のパフォーマンス カウンターは次のようになります。

public class MyPerformanceCounter
{
    TableStorageHelper tableHelper;
    PerformanceCounter myCounter;

    public MyPerformanceCounter()
    {
        this.tableHelper = new TableStorageHelper();
    }
    public void run()
    {
        if (!PerformanceCounterCategory.Exists("Processor"))
        {
            Trace.WriteLine("Object Processor does not exist!");
            return;
        }

        if (!PerformanceCounterCategory.CounterExists(@"% Processor Time", "Processor"))
        {
            Trace.WriteLine(@"Counter % Processor Time does not exist!");
            return;
        }

        this.myCounter = new PerformanceCounter("Processor", @"% Processor Time", @"_Total");

        while (true)
        {

            try
            {
                float value = this.myCounter.NextValue();
                Trace.WriteLine(@"Current value of Processor, %Processor Time, _Total= " + value.ToString());
                tableHelper.persist(value);
            }

            catch
            {
                Trace.WriteLine(@"_Total instance does not exist!");
                continue;
            }
            // grep all 2 seconds new data (if exists)
            Thread.Sleep(2000);
        }
    } 

WebService をローカルで実行すると、正常に動作します。しかし、それをクラウドにデプロイすると、「メトリックの取得中にサーバーでエラーが発生しました (InternalError)。操作を再試行してください。」というエラー メッセージが表示されます。デプロイ プロセスはエラーを返しませんでした。Azure admin WebGui でのみ、このエラーが表示されるか、WebService を使用したい場合にのみ発生します。

編集:私は自分の役割の csdef ファイルに追加しました:

<Runtime executionContext="elevated"></Runtime>

これで、Web サービスが開始、実行され、応答が送信されます。パフォーマンス カウンターは、テーブル ストレージにテーブルを作成します (したがって、webrole onstart のスレッドが実行されます)。ただし、パフォーマンス データはテーブル ストレージに書き込まれません。

EDIT2:テーブルストレージのコードは次のとおりです。

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WCFServiceWebRole1
{
public class TableStorageHelper
{
    CloudTable table;
    public TableStorageHelper() { init(); }

    private void init()
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("mytable"));
        // Create the table client.
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        // Create the table if it doesn't exist.
        this.table = tableClient.GetTableReference("mytable");
        this.table.CreateIfNotExists();
    }

    public void persist(float ProcessorValue)
    {
        PerformanceEntity entity = new PerformanceEntity();
        entity.ProcessorValue = ProcessorValue.ToString();

        TableOperation insertOperation = TableOperation.Insert(entity);

        // Execute the insert operation.
        this.table.Execute(insertOperation);

    }

}

}

永続性エンティティ:

using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WCFServiceWebRole1
{
    public class PerformanceEntity : TableEntity
    {
        public PerformanceEntity()
        {
            this.PartitionKey = RoleEnvironment.CurrentRoleInstance.Id;
            this.RowKey = DateTime.Now.ToString();
        }

        public string ProcessorValue { get; set; }

    }
}

これを Azure クラウドで実行できない理由を知っている人はいますか?

4

0 に答える 0