Опубликован: 30.05.2011 | Доступ: свободный | Студентов: 2407 / 234 | Оценка: 4.12 / 4.41 | Длительность: 12:00:00
Специальности: Программист, Архитектор программного обеспечения
Теги:
Самостоятельная работа 7:
Работа с Windows AzureQueue
Приложение В Worker.cs
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; namespace WorkerRole1 { public class WorkerRole : RoleEntryPoint { public override void Run() { var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); var queueClient = storageAccount.CreateCloudQueueClient(); var queue = queueClient.GetQueueReference("messagequeue"); while (true) { Thread.Sleep(10000); if (queue.Exists()) { var msg = queue.GetMessage(); if (msg != null) { Trace.TraceInformation(string.Format("Сообщение '{0}' обработано.", msg.AsString)); queue.DeleteMessage(msg); } } } } public override bool OnStart() { ServicePointManager.DefaultConnectionLimit = 12; CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); }); return base.OnStart(); } } }
Приложение Г AzureQueueSample2.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient; using Microsoft.WindowsAzure.ServiceRuntime; namespace WebRole1 { public partial class AzureQueueSample2 : System.Web.UI.Page { CloudStorageAccount storageAccount = null; CloudQueue cloudQueue = null; CloudQueueClient queueClient = null; protected void Page_Load(object sender, EventArgs e) { this.storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); this.queueClient = storageAccount.CreateCloudQueueClient(); this.cloudQueue = this.queueClient.GetQueueReference("myqueue"); this.cloudQueue.CreateIfNotExist(); this.DisplayMesssages(); } private void DisplayMesssages() { var msgs = this.cloudQueue.PeekMessages(5); var cloudList = new List<string>(); foreach (var msg in msgs) { cloudList.Add("Message ID: " + msg.Id + "; Message: " + msg.AsString + "; Message insertion time: " + msg.InsertionTime); } this.Repeater1.DataSource = cloudList; this.Repeater1.DataBind(); } protected void bnt_addmessage_Click(object sender, EventArgs e) { CloudQueueMessage msg = new CloudQueueMessage(this.tb_insert.Text); this.cloudQueue.AddMessage(msg); this.DisplayMesssages(); } protected void btn_addbinary_Click(object sender, EventArgs e) { CloudQueueMessage msg = new CloudQueueMessage(File.ReadAllBytes(fu_insertbinary.FileName)); this.cloudQueue.AddMessage(msg); this.DisplayMesssages(); } protected void btn_getmessage_Click(object sender, EventArgs e) { CloudQueueMessage msg = this.cloudQueue.GetMessage(); this.lb_retrievedmessage.Text = "Retrieved message: " + msg.AsString; this.cloudQueue.DeleteMessage(msg); this.DisplayMesssages(); } } }