ÕªÒª£ºÎÞÔðÈÎWindows Azure SDK .NET¿ª·¢ÈëÃÅÆª£¬½«´øÀ´Ò»ÏµÁлù´¡ÎÄÕ£º´ÓWindows Azure¿ª·¢Ç°×¼±¸¹¤×÷¡¢Ê¹ÓÃAzure AD ¹ÜÀíÓû§ÐÅÏ¢¡¢´´½¨¹ÜÀí¡°ÔÆ¡±·þÎñ¡¢µ½Ê¹ÓÃBlob Storage·þÎñµÈ£¬°ïÖú¶ÁÕßÇáÒ×ÉÏÊÖʹÓÃÕâÌ׿ª·¢¹¤¾ß¡£
ÈôÏë»Ø¹ËWindows Azure SDK .NET¿ª·¢ÍêÕûÎÄÕ£¬¿Éµã»÷ÒÔÏÂÁ´½Ó¡£¶ø±¾ÎĽ«¾Í¡°Ê¹ÓÃQueue Storage·þÎñ¡±½øÐнéÉÜ¡£
Azure ¶ÓÁд洢ÊÇÒ»ÏîÓÃÓÚ´æ´¢´óÁ¿ÏûÏ¢µÄ·þÎñ£¬ Óû§¿ÉÒÔͨ¹ý¾ÑéÖ¤µÄºô½Ð£¬Ê¹Óà HTTP »ò HTTPS ´Ó ÊÀ½çÈκεط½·ÃÎÊÕâЩÏûÏ¢¡£Ò»Ìõ¶ÓÁÐÏûÏ¢µÄ ´óС×î¶à¿É´ï 64 KB£¬Ò»¸ö¶ÓÁÐÖпÉÒÔ°üº¬Êý°ÙÍòÌõÏûÏ¢£¬×î¶à¿É´ï ´æ´¢ÕÊ»§µÄ×ÜÈÝÁ¿ÏÞÖÆ¡£´æ´¢ÕÊ»§¿ÉÒÔÈÝÄɸߴï 200 TB µÄ Blob¡¢¶ÓÁкͱíÊý¾Ý¡£
¶ÓÁд洢µÄ³£¼ûÓÃ;°üÀ¨£º
- ´´½¨»ýѹ¹¤×÷ÒÔ½øÐÐÒì²½´¦Àí
-
½«ÏûÏ¢´Ó Azure Web ½ÇÉ«´«µÝµ½ Azure ¸¨Öú½ÇÉ«
ÏÂÃæÊÇ×é¼þ¸ÅÄîͼ£º

ÔÚ¿ª·¢Ç°ÎÒÃÇÐèÒªÁ˽âµÄ¸ÅÄîÓÐ
- URL ¸ñʽ£º¿ÉʹÓÃÒÔÏ URL ¸ñʽ¶Ô¶ÓÁнøÐÐѰַ£º http://<storage account>.queue.core.chinacloudapi.cn/<queue>
-
Storage ÕÊ»§£º¶Ô Azure ´æ´¢¿Õ¼ä½øÐеÄËùÓзÃÎʶ¼ÒªÍ¨¹ý´æ´¢ÕÊ»§Íê³É¡£
-
¶ÓÁУºÒ»¸ö¶ÓÁаüº¬Ò»×éÏûÏ¢¡£ËùÓÐÏûÏ¢¶¼±ØÐëÔÚ¶ÓÁÐÖС£
-
ÏûÏ¢£ºÒ»ÌõÏûÏ¢£¨²»¹Ü²ÉÓúÎÖÖ¸ñʽ£©µÄ×î´ó´óСΪ 64 KB¡£
ÎÒÃÇÐèÒª´ÓNuGet»ñÈ¡WindowsAzure.Storage¿â£¬¸Ã¿âµÄÏîÄ¿µØÖ·ÊÇ£ºhttps://www.nuget.org/packages/WindowsAzure.Storage£¬ÒýÓÃÍê³Éºó£¬ÎÒÃǽ¨Á¢±¾ÕµĿØÖÆÆ÷£ºStorageQueueController£¬¸Ã¿ØÖÆÆ÷ÓÐÈçÏÂAction
- Index
-
Create
-
Delete
-
Insert
-
DeQueue
-
Clear
ÎÒÃǵĿØÖÆÆ÷ÊÇÕâÑùµÄ£º
[Authorize] public class StorageQueueController : Controller { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("Microsoft.WindowsAzure.Storage"));
CloudQueueClient queueClient = null; public StorageQueueController()
{
queueClient = storageAccount.CreateCloudQueueClient();
}
}
|
7.1 Index Áгö¶ÓÁÐ×ÊÔ´
¿ØÖÆÆ÷µÄ´úÂëÈçÏ£º
public ActionResult Index() { var queues = queueClient.ListQueues(); return View(queues); } |
¶ÔÓ¦µÄView£º
@model IEnumerable<Microsoft.WindowsAzure.Storage.Queue.CloudQueue> @{ ViewBag.Title = "Index"; }
<h2>Index</h2> <p> @Html.ActionLink("´´½¨ÐµĶÓÁÐ", "Create") </p> <table class="table"> <tr> <th>Name</th> <th>StorageUri</th> <th>ÏûÏ¢ÊýÁ¿</th> </tr> @foreach (var item in Model) { <tr> <td> @Html.ActionLink(@item.Name, "DeQueue", new { name = item.Name }) </td> <td>@item.StorageUri</td> <td> @{ item.FetchAttributes();
} @item.ApproximateMessageCount </td>
<td> @Html.ActionLink("²åÈëÏûÏ¢", "Insert", new { name = item.Name }) | @Html.ActionLink("ɾ³ý", "Delete", new { name = item.Name }) </td> </tr> }
</table>
|
ÔËÐеĽá¹ûÈçͼ£º

7.2 Create´´½¨¶ÓÁÐ
¿ØÖÆÆ÷µÄ´úÂëºÜ¼òµ¥£º
<span style="font-weight: normal;">[HttpPost] public ActionResult Create(string name) { CloudQueue queue = queueClient.GetQueueReference(name); queue.CreateIfNotExists();
return RedirectToAction("Index"); }</span> |
¶ÔÓ¦µÄViewΪ£º
<span style="font-weight: normal;"> @{ ViewBag.Title = "Create"; } <h2>Create</h2>
@using (Html.BeginForm()) { @Html.AntiForgeryToken()
<div class="form-horizontal"> <h4>CloudTable</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> <div class="col-md-offset-2 col-md-10"> @Html.Label("¶ÓÁÐÃû³Æ") @Html.TextBox("name") </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> }
<div> @Html.ActionLink("Back to List", "Index") </div></span> |
ÔËÐнá¹ûÈçͼ£º

´´½¨³É¹¦ºóÌø×ªµ½IndexÒ³Ãæ£º

7.3 Deleteɾ³ý¶ÓÁÐ
ɾ³ý¶ÓÁеĿØÖÆÆ÷·Ç³£¼òµ¥¡£
public ActionResult Delete(string name) { queueClient.GetQueueReference(name).Delete(); return RedirectToAction("Index"); } |
7.4 Insert ²åÈëÏûÏ¢
¿ØÖÆÆ÷µÄ´úÂë±È½ÏÃ÷È·¡£
[HttpPost] public ActionResult Insert(string name, int count) { CloudQueue queue = queueClient.GetQueueReference(name); for (var i = 0; i <= count; i++)
{
var msg = new CloudQueueMessage(Guid.NewGuid().ToString(), string.Format("{0}.{1}", name, DateTime.Now));
msg.SetMessageContent(string.Format("StorageQueueController:{0}", DateTime.Now.Ticks));
queue.AddMessage(msg, timeToLive: new TimeSpan(3, 0, 0)); }
return RedirectToAction("DeQueue", new { name = name }); } |
ʵÀýCloudQueueMessageºóÎÒÃÇ¿ÉÒÔÌí¼ÓÏûÏ¢ÕýÎÄÒÔÃèÊö¸ü¶àÐÅÏ¢¡£

ÏûÏ¢²åÈë³É¹¦ºó»áÌø×ªµ½ÏûÏ¢»ñÈ¡µÄÒ³Ãæ£º

7.5 DeQueue ÏûÏ¢»ñÈ¡
ÏûÏ¢»ñÈ¡²ÉÓÃjQueryµÄAjaxÀ´²Ù×÷£¬¿ØÖÆÆ÷Ϊ£º
public ActionResult DeQueue(string name) { ViewBag.Name = name; return View(); } |
¶ÔÓ¦µÄViewÖÐ×¢ÒâÎÒÃDzÉÓÃAjaxµÄ·½Ê½»òÐí¶ÓÁÐÐÅÏ¢£¬²¢½«µÃµ½µÄÐÅÏ¢¶¯Ì¬Ìí¼Óµ½TableÖС£
@{ ViewBag.Title = "DeQueue"; }<h2>DeQueue</h2> @section scripts{ <script> $(function () { $("#getmessage").click(function () { $.ajax( { method: "POST", url: "@Url.Action("GetMessage", "StorageQueue")", data:{name:"@ViewBag.Name"} }).done(function(msg){ var trhtml = "<tr><td>" + msg.Id + "</td><td>" + msg.AsString + "</td><td>" + msg.ExpirationTime + "</td><td>" + msg.DequeueCount + "</td></tr>"; $("tbody tr:last").after(trhtml); }); }); }) </script> } <h2>@ViewBag.Path</h2>
@Html.Label("ÏûÏ¢½ûÖ¹ÖÜÆÚ£¬0±íʾÁ¢¼´É¾³ý") @Html.TextBox("count", 10, new { id = "count" }) <input type="button" value="»ñÈ¡ÏûÏ¢" id="getmessage" /> <table class="table"> <tr> <th>ÏûÏ¢ID</th> <th>ÏûÏ¢ÕýÎÄ</th> <th>µ½ÆÚʱ¼ä</th> <th>ÒÆ³ý´ÎÊý</th> </tr> <tbody></tbody> </table> |
ÎÒÃǵÄAjax´úÂëÈçÏ£º
[HttpPost] public JsonResult GetMessage(string name) { CloudQueue queue = queueClient.GetQueueReference(name); CloudQueueMessage retrievedMessage = queue.GetMessage(); queue.DeleteMessage(retrievedMessage); return Json(retrievedMessage); } |
ÔËÐеÄͼΪ£º

7.6 ClearÇå³ýÏûÏ¢
´úÂë·Ç³£¼òµ¥£¬¾ÍÕâÑù°É¡£
public ActionResult Clear(string name) { queueClient.GetQueueReference(name).Clear();
return RedirectToAction("Index"); } |
×÷Õß¼ò½é
ÍõÔ¥Ï裬ÉϺ£ÖÂʤÐÅÏ¢¼¼ÊõÓÐÏÞ¹«Ë¾¿ª·¢²¿¾Àí£¬Î¢Èí×îÓмÛֵר¼Ò£¨Microsoft MVP£©¡£ÔøÔÚ¸÷ÖÖÀàÐÍÆóÒµ×ö±à³Ì¼¼Êõ¹¤×÷£¬´Ó´úÂ빤È˵½¼Ü¹¹Éè¼Æ£¬´ÓCSµ½BS£¬´Ó¾²Ì¬ÓïÑÔµ½¶¯Ì¬ÓïÑÔ£¬´ÓÆóÒµÓ¦Óõ½Òƶ¯»¥ÁªÍø¡£×î½ü3ÄêÖ÷³ÖʵʩÁ˶à¸ö´óÐÍBIÏîÄ¿ºÍAzureÏîÄ¿¡£ |