技术开发 频道

打造Windows Azure云服务应用程序

  现在,我们可以向Default.aspx.cs文件中添加代码,以便创建新的Calculation对象,并将其保存到Table存储器。这可以通过向Page_Load方法添加代码,并启动Calculation按钮的单击事件处理程序来完成。

  Default.aspx.cs (Part 1)
  
using Microsoft.Samples.ServiceHosting.StorageClient;
  
using System.Data.Services.Client;
  
// ...
  protected void Page_Load(object sender, EventArgs e)
  {
  
try
  {
  StorageAccountInfo accountInfo
= StorageAccountInfo.GetAccountInfoFromConfiguration("TableStorageEndpoint");
  
// Not required in development since the table is created
  
// manually but is needed in the cloud to create the tables
  
// the first time.
  TableStorage.CreateTablesFromModel(typeof(CalculationDataServiceContext), accountInfo);
  }
  
catch (DataServiceRequestException ex)
  {
  
// Unable to connect to table storage. In development
  
// verify the service is running. In the cloud check that
  
// the config values are correct.
  
// Handle error
  }
  }
  
protected void cmdStartCalculation_Click(object sender, EventArgs e)
  {
  StorageAccountInfo accountTableInfo
= StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration();
  CalculationDataServiceContext calculationDSContext
= new CalculationDataServiceContext(accountTableInfo);
  lblCalculationKey.Text
=
  calculationDSContext.AddCalculation(
int.Parse(txtValueOne.Text), int.Parse(txtValueTwo.Text));
  lblCalculationKey.Visible
= true;
  }

 

  当运行该应用程序时,我们可以输入两个数值,将其存入Table存储器,然后返回RowKey,以便后来之用。下一步要做的是通过Queue存储器发送一个消息,来检索这两个数值,并求和。

  5.使用Windows Azure Queue存储器

  为了给Work Role发送消息,我们需要首先向Default.aspx.cs文件插入代码,以便将消息添加到该Queue。这些代码需要放入Calculation对象创建和RowKey返回时的按钮单击事件中。为了能够检索正确的值进行计算,Worker Role需要知道PartitionKey和RowKey。对本例来说,PartitionKey被硬编码到TEST,所以我们只需传递RowKey即可。为了获得更加健壮的应用程序,我们应该将这两个值都传递给向Worker Role。

  Default.aspx.cs (Part 2)
  
protected void cmdStartCalculation_Click(object sender, EventArgs e)
  {
  
// ...
  StorageAccountInfo accountQueueInfo = StorageAccountInfo.GetDefaultQueueStorageAccountFromConfiguration();
  QueueStorage qs
= QueueStorage.Create(accountQueueInfo);
  MessageQueue queue
= qs.GetQueue("calculations");
  
if (!queue.DoesQueueExist())
  queue.CreateQueue();
  Message msg
= new Message(lblCalculationKey.Text);
  queue.PutMessage(msg);
  }

 

0
相关文章