ตอนที่ 13 : How to use .Net (Asp.net) Create a message Queue - สร้างคิวใหม่
ตอนที่ 13 : How to use .Net (Asp.net) Create a message Queue - สร้างคิวใหม่ บทความนี้จะเป็นตัวอย่างการเขียน .NET (ASP.Net) กับ Queue Service Storage ของ Windows Azure โดยจะยกตัวอย่างขึ้นมา 2 ตัวอย่าง คือการใช้ ASP.Net กับ Azure library เพื่อสร้าง Message Queue และการอ่านจำนวน Queue ทั้งหมด
Syntax การสร้าง Queue
// Create the queue client
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
// Retrieve a reference to a queue.
CloudQueue queue = queueClient.GetQueueReference("myqueue");
// Create the queue if it doesn't already exist.
queue.CreateIfNotExists();
// Create a message and add it to the queue.
CloudQueueMessage message = new CloudQueueMessage("Hello World! 1");
queue.AddMessage(message);
Example 1 เขียน .NET (ASP.Net) เพื่อสร้าง Queue ไปยัง Queue Storage บน Windows Azure
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Queue;
namespace myWebApp
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Create the connectionstring
String StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=[yourAccount];AccountKey=[yourKey]";
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageConnectionString);
// Create the queue client
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
// Retrieve a reference to a queue.
CloudQueue queue = queueClient.GetQueueReference("myqueue");
// Create the queue if it doesn't already exist.
queue.CreateIfNotExists();
// Create a message and add it to the queue.
CloudQueueMessage message = new CloudQueueMessage("Hello World! 1");
queue.AddMessage(message);
this.lblResult.Text = "Queue message has been added.";
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Queue;
namespace myWebApp
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Create the connectionstring
String StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=[yourAccount];AccountKey=[yourKey]";
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageConnectionString);
// Create the queue client
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
// Retrieve a reference to a queue.
CloudQueue queue = queueClient.GetQueueReference("myqueue");
// Fetch the queue attributes.
queue.FetchAttributes();
// Retrieve the cached approximate message count.
int? cachedMessageCount = queue.ApproximateMessageCount;
// Display number of messages.
this.lblResult.Text = cachedMessageCount.ToString();
}
}
}