شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة ش.ش.و.

Integrated Applications Programming Company

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