#眉標=Windows Azure、ASP.NET、Cloud #副標=Windows Azure平台開發技術(1) #大標=雲端應用程式開發初體驗 #作者=文/圖 黃忠成 ===<反灰>============= protected void Timer1_Tick(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToString(); } ================ ===<反灰>============= using System; using System.Collections.Generic; using System.Threading; using System.Linq; using System.Text; using Microsoft.ServiceHosting.ServiceRuntime; using Microsoft.Samples.ServiceHosting.StorageClient; namespace WorkerRole1 { public class WorkerRole : RoleEntryPoint { public override void Start() { // This is a sample worker implementation. Replace with your logic. RoleManager.WriteToLog("Information", "Worker Process entry point called"); while (true) { Thread.Sleep(5000); string accountName = "devstoreaccount1"; string accountKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="; string address = "http://127.0.0.1:10001"; QueueStorage service = QueueStorage.Create(new Uri(address), true, accountName, accountKey); MessageQueue queue = service.GetQueue("messages"); if (!queue.DoesQueueExist()) continue; else { Message msg = queue.GetMessage(); string content = msg.ContentAsString(); if (content.StartsWith("WORK:")) { queue.DeleteMessage(msg); content = content.Substring(5); queue.PutMessage(new Message("RESP:"+content.ToUpper())); } } RoleManager.WriteToLog("Information", "Working"); } } public override RoleStatus GetHealthStatus() { // This is a sample worker implementation. Replace with your logic. return RoleStatus.Healthy; } } } ================ ===<反灰>============= protected void Button1_Click(object sender, EventArgs e) { string accountName = "devstoreaccount1"; string accountKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="; string address = "http://127.0.0.1:10001"; QueueStorage service = QueueStorage.Create(new Uri(address), true, accountName, accountKey); MessageQueue queue = service.GetQueue("messages"); if (!queue.DoesQueueExist()) queue.CreateQueue(); Message msg = new Message("WORK:"+TextBox1.Text); queue.PutMessage(msg); msg = queue.PeekMessage(); while (true) { if (msg != null) { if (msg.ContentAsString().StartsWith("RESP:")) { msg = queue.GetMessage(); TextBox1.Text = msg.ContentAsString().Substring(5); queue.DeleteMessage(msg); break; } } msg = queue.PeekMessage(); System.Threading.Thread.Sleep(500); } queue.DeleteQueue(); } ================