6.将它们放在一起
最后,我们需要向Default.aspx.cs添加代码,以便用户检索计算结果。为此,需要向检索计算结果的按钮的单击事件处理程序中添加如下所示的代码。
Default.aspx.cs (Part 3)
protected void cmdRetrieveCalculation_Click(object sender, EventArgs e)
{
// Create account info objects for accessing table storage
StorageAccountInfo accountTableInfo = StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration();
CalculationDataerviceContext calcDSContext = new CalculationDataServiceContext(accountTableInfo);
// Retrieve the Calculation entity from Table storage
Calculation calc = (from c in calcDSContext.Calculations
where c.PartitionKey == "TEST"
&& c.RowKey == txtCalculationKey.Text
select c).FirstOrDefault();
lblCalculatedValue.Text = calc.CalcValue.ToString();
lblCalculatedValue.Visible = true;
}
protected void cmdRetrieveCalculation_Click(object sender, EventArgs e)
{
// Create account info objects for accessing table storage
StorageAccountInfo accountTableInfo = StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration();
CalculationDataerviceContext calcDSContext = new CalculationDataServiceContext(accountTableInfo);
// Retrieve the Calculation entity from Table storage
Calculation calc = (from c in calcDSContext.Calculations
where c.PartitionKey == "TEST"
&& c.RowKey == txtCalculationKey.Text
select c).FirstOrDefault();
lblCalculatedValue.Text = calc.CalcValue.ToString();
lblCalculatedValue.Visible = true;
}
运行该程序,现在我们可以进行计算并检索结果了。记住,我们的这个服务每10秒才会检查一次队列,所以在检索计算结果之前需要稍等片刻。
图6 完工后的应用程序截屏
7.结束语
虽然在云中建立一个简单的加法机是一件无足轻重的事情,但是我们却可以从中了解云服务应用程序的许多重要知识:如何创建一个新的云服务,以及如何设置配置文件来使用Azure Storage,通过Table存储器检索和更新数据的方法,使用Queue存储器在云服务角色间传递消息的方式,以及如何利用Azure SDK提供的StorageClient项目迅速开发Azure Storage服务。