Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Storage

Public general use code classes and xml files that we've compiled and used over the years:

Azure Cloud related support functions.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Text.RegularExpressions;
   6:  using System.Threading.Tasks;
   7:  using Microsoft.WindowsAzure.Storage;
   8:  using Microsoft.WindowsAzure.Storage.Auth;
   9:  using Microsoft.WindowsAzure.Storage.Queue;
  10:  using System.Configuration;
  11:   
  12:  namespace Ia.Cl.Model.Azure
  13:  {
  14:      ////////////////////////////////////////////////////////////////////////////
  15:   
  16:      /// <summary publish="true">
  17:      /// Azure Cloud related support functions.
  18:      /// </summary>
  19:      /// <value>
  20:      /// 
  21:      /// Naming Stores:
  22:      /// Experimentation suggests the rules for naming a queue include: (a) use only lower case letters, (b) digits are allowed anywhere, and (c) internal single hyphens are okay too, but (d) name should not contain any spaces (e) nor any punctuation (other than hyphen).
  23:      /// http://blog.codingoutloud.com/2010/05/06/azure-error-one-of-the-request-inputs-is-out-of-range/
  24:      /// 
  25:      /// In web.config and app.config include:
  26:      /// <appSettings>
  27:      ///    <add key="applicationGuid" value="*" />
  28:      ///    <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=*;AccountKey=*;QueueEndpoint=http://*.queue.core.windows.net;" />
  29:      /// </appSettings>
  30:      /// </value>
  31:      /// 
  32:      /// <remarks> 
  33:      /// Copyright � 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  34:      ///
  35:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  36:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  37:      ///
  38:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  39:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  40:      /// 
  41:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  42:      /// 
  43:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  44:      /// </remarks> 
  45:   
  46:      ////////////////////////////////////////////////////////////////////////////
  47:   
  48:      public partial class Storage
  49:      {
  50:          private static CloudStorageAccount storageAccount;
  51:          private static CloudQueueClient queueClient;
  52:          private static CloudQueue queue;
  53:          private static CloudQueueMessage message;
  54:   
  55:          ////////////////////////////////////////////////////////////////////////////
  56:   
  57:          /// <summary>
  58:          ///
  59:          /// </summary>
  60:          public Storage() { }
  61:   
  62:          ////////////////////////////////////////////////////////////////////////////
  63:   
  64:          /// <summary>
  65:          ///
  66:          /// </summary>
  67:          public static void WriteContent(string queueName, string content)
  68:          {
  69:              // Retrieve storage account from connection string
  70:              storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
  71:   
  72:              // Create the queue client
  73:              queueClient = storageAccount.CreateCloudQueueClient();
  74:   
  75:              // Retrieve a reference to a queue
  76:              queue = queueClient.GetQueueReference(queueName);
  77:   
  78:              // Create the queue if it doesn't already exist
  79:              queue.CreateIfNotExists();
  80:   
  81:              // Create a message and add it to the queue.
  82:              message = new CloudQueueMessage(content);
  83:              queue.AddMessage(message);
  84:          }
  85:   
  86:          ////////////////////////////////////////////////////////////////////////////
  87:   
  88:          /// <summary>
  89:          ///
  90:          /// </summary>
  91:          public static void WriteContent(string content)
  92:          {
  93:              string queueName;
  94:   
  95:              queueName = ConfigurationManager.AppSettings["applicationGuid"].ToString();
  96:   
  97:              WriteContent(queueName, content);
  98:          }
  99:   
 100:          ////////////////////////////////////////////////////////////////////////////
 101:   
 102:          /// <summary>
 103:          ///
 104:          /// </summary>
 105:          public static string PeekContent(string queueName)
 106:          {
 107:              // Retrieve storage account from connection string
 108:              storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
 109:   
 110:              // Create the queue client
 111:              queueClient = storageAccount.CreateCloudQueueClient();
 112:   
 113:              // Retrieve a reference to a queue
 114:              queue = queueClient.GetQueueReference(queueName);
 115:   
 116:              // Create the queue if it doesn't already exist
 117:              queue.CreateIfNotExists();
 118:   
 119:              message = null;
 120:   
 121:              if (queue.Exists())
 122:              {
 123:                  // Peek at the next message
 124:                  message = queue.PeekMessage();
 125:              }
 126:   
 127:              return (message != null) ? message.AsString : null;
 128:          }
 129:   
 130:          ////////////////////////////////////////////////////////////////////////////
 131:   
 132:          /// <summary>
 133:          ///
 134:          /// </summary>
 135:          public static string PeekContent()
 136:          {
 137:              string queueName;
 138:   
 139:              queueName = ConfigurationManager.AppSettings["applicationGuid"].ToString();
 140:   
 141:              return PeekContent(queueName);
 142:          }
 143:   
 144:          ////////////////////////////////////////////////////////////////////////////
 145:   
 146:          /// <summary>
 147:          ///
 148:          /// </summary>
 149:          public static string UpdateContent(string queueName, string updatedContent)
 150:          {
 151:              // Retrieve storage account from connection string
 152:              storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
 153:   
 154:              // Create the queue client
 155:              queueClient = storageAccount.CreateCloudQueueClient();
 156:   
 157:              // Retrieve a reference to a queue
 158:              queue = queueClient.GetQueueReference(queueName);
 159:   
 160:              // Create the queue if it doesn't already exist
 161:              queue.CreateIfNotExists();
 162:   
 163:              // Get the message from the queue and update the message contents.
 164:              message = queue.GetMessage();
 165:              message.SetMessageContent(updatedContent);
 166:              queue.UpdateMessage(message,
 167:                  TimeSpan.FromSeconds(0.0),  // Make it visible immediately.
 168:                  MessageUpdateFields.Content | MessageUpdateFields.Visibility);
 169:   
 170:              return message.AsString;
 171:          }
 172:   
 173:          ////////////////////////////////////////////////////////////////////////////
 174:   
 175:          /// <summary>
 176:          ///
 177:          /// </summary>
 178:          public static string UpdateContent(string updatedContent)
 179:          {
 180:              string queueName;
 181:   
 182:              queueName = ConfigurationManager.AppSettings["applicationGuid"].ToString();
 183:   
 184:              return UpdateContent(queueName, updatedContent);
 185:          }
 186:   
 187:          ////////////////////////////////////////////////////////////////////////////
 188:   
 189:          /// <summary>
 190:          ///
 191:          /// </summary>
 192:          public static void DeleteContent(string queueName)
 193:          {
 194:              // Retrieve storage account from connection string
 195:              storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
 196:   
 197:              // Create the queue client
 198:              queueClient = storageAccount.CreateCloudQueueClient();
 199:   
 200:              // Retrieve a reference to a queue
 201:              queue = queueClient.GetQueueReference(queueName);
 202:   
 203:              message = null;
 204:   
 205:              if (queue.Exists())
 206:              {
 207:                  // Get the next message
 208:                  message = queue.GetMessage();
 209:   
 210:                  // Process the message in less than 30 seconds, and then delete the message
 211:                  queue.DeleteMessage(message);
 212:              }
 213:          }
 214:   
 215:          ////////////////////////////////////////////////////////////////////////////
 216:   
 217:          /// <summary>
 218:          ///
 219:          /// </summary>
 220:          public static void DeleteContent()
 221:          {
 222:              string queueName;
 223:   
 224:              queueName = ConfigurationManager.AppSettings["applicationGuid"].ToString();
 225:   
 226:              DeleteContent(queueName);
 227:          }
 228:   
 229:          ////////////////////////////////////////////////////////////////////////////
 230:   
 231:          /// <summary>
 232:          ///
 233:          /// </summary>
 234:          public static void DeleteQueue(string queueName)
 235:          {
 236:              // Retrieve storage account from connection string
 237:              storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
 238:   
 239:              // Create the queue client
 240:              queueClient = storageAccount.CreateCloudQueueClient();
 241:   
 242:              // Retrieve a reference to a queue
 243:              queue = queueClient.GetQueueReference(queueName);
 244:   
 245:              if (queue.Exists())
 246:              {
 247:                  // Delete the queue.
 248:                  queue.Delete();
 249:              }
 250:          }
 251:   
 252:          ////////////////////////////////////////////////////////////////////////////
 253:   
 254:          /// <summary>
 255:          ///
 256:          /// </summary>
 257:          public static void Test()
 258:          {
 259:              // Retrieve storage account from connection string
 260:              storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
 261:   
 262:              // Create the queue client
 263:              queueClient = storageAccount.CreateCloudQueueClient();
 264:   
 265:              // Retrieve a reference to a queue
 266:              queue = queueClient.GetQueueReference("myqueue");
 267:   
 268:              // Create the queue if it doesn't already exist
 269:              queue.CreateIfNotExists();
 270:   
 271:              // Create a message and add it to the queue.
 272:              message = new CloudQueueMessage("Hello, World");
 273:              queue.AddMessage(message);
 274:   
 275:   
 276:              // Peek at the next message
 277:              message = queue.PeekMessage();
 278:              // Display message.
 279:              Console.WriteLine(message.AsString);
 280:   
 281:   
 282:              // Get the message from the queue and update the message contents.
 283:              message = queue.GetMessage();
 284:              message.SetMessageContent("Updated contents.");
 285:              queue.UpdateMessage(message,
 286:                  TimeSpan.FromSeconds(0.0),  // Make it visible immediately.
 287:                  MessageUpdateFields.Content | MessageUpdateFields.Visibility);
 288:   
 289:              // Get the next message
 290:              message = queue.GetMessage();
 291:   
 292:              //Process the message in less than 30 seconds, and then delete the message
 293:              queue.DeleteMessage(message);
 294:   
 295:              // Delete the queue.
 296:              queue.Delete();
 297:          }
 298:   
 299:          ////////////////////////////////////////////////////////////////////////////
 300:          ////////////////////////////////////////////////////////////////////////////
 301:      }
 302:  }