我正在使用带有 Windows Azure 工具的 Visual Studio 2012(2012 年 10 月)。我有 WebService,它对 Windows Azure Cloud 上的实例做了几件事。这工作正常。
现在我想添加一个性能计数器,它以某个时间间隔(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 Cloud 上运行它?