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

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

MSMQ (Microsoft Message Queuing) Support class.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Messaging;
   4:   
   5:  namespace Ia.Cl.Model
   6:  {
   7:      ////////////////////////////////////////////////////////////////////////////
   8:   
   9:      /// <summary publish="true">
  10:      /// MSMQ (Microsoft Message Queuing) Support class.
  11:      /// </summary>
  12:      /// <remarks> 
  13:      /// Copyright © 2015-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  14:      ///
  15:      /// 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
  16:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  17:      ///
  18:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  19:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  20:      /// 
  21:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  22:      /// 
  23:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  24:      /// </remarks> 
  25:   
  26:      ////////////////////////////////////////////////////////////////////////////
  27:   
  28:      /// <summary>
  29:      ///
  30:      /// </summary>
  31:      public static class Msmq
  32:      {
  33:          ////////////////////////////////////////////////////////////////////////////
  34:   
  35:          /// <summary>
  36:          /// References public queues.
  37:          /// </summary>
  38:          [Obsolete("Code does not work property and/or is not tested. ")]
  39:          public static void SendPublic(string queueName, string label, string text)
  40:          {
  41:              string path;
  42:   
  43:              path = @"FormatName:DIRECT=OS:Ia\" + queueName;
  44:   
  45:              using (MessageQueue mq = new MessageQueue())
  46:              {
  47:                  try
  48:                  {
  49:                      if (MessageQueue.Exists(path)) mq.Path = path;
  50:                      else MessageQueue.Create(path);
  51:   
  52:                      mq.Send(text, label);
  53:                  }
  54:                  catch (MessageQueueException mqe)
  55:                  {
  56:                  }
  57:              }
  58:          }
  59:   
  60:          ////////////////////////////////////////////////////////////////////////////
  61:   
  62:          /// <summary>
  63:          ///
  64:          /// </summary>
  65:          [Obsolete("Code does not work property and/or is not tested. ")]
  66:          public static void RecievePublic(string queueName, out string label, out string body)
  67:          {
  68:              System.Messaging.Message m;
  69:   
  70:              m = RecievePublic(queueName);
  71:   
  72:              if (m != null)
  73:              {
  74:                  label = m.Label;
  75:                  body = (string)m.Body;
  76:              }
  77:              else
  78:              {
  79:                  label = body = string.Empty;
  80:              }
  81:          }
  82:   
  83:          ////////////////////////////////////////////////////////////////////////////
  84:   
  85:          /// <summary>
  86:          ///
  87:          /// </summary>
  88:          [Obsolete("Code does not work property and/or is not tested. ")]
  89:          public static System.Messaging.Message RecievePublic(string queueName)
  90:          {
  91:              string path;
  92:              System.Messaging.Message m;
  93:   
  94:              path = @".\" + queueName;
  95:   
  96:              using (MessageQueue mq = new MessageQueue())
  97:              {
  98:                  try
  99:                  {
 100:                      if (MessageQueue.Exists(path))
 101:                      {
 102:                          mq.Path = path;
 103:   
 104:                          mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
 105:   
 106:                          m = mq.Receive();
 107:                      }
 108:                      else
 109:                      {
 110:                          m = null;
 111:                      }
 112:                  }
 113:                  catch (MessageQueueException mqe)
 114:                  {
 115:                      m = null;
 116:                  }
 117:              }
 118:   
 119:              return m;
 120:          }
 121:   
 122:          ////////////////////////////////////////////////////////////////////////////
 123:          ////////////////////////////////////////////////////////////////////////////
 124:   
 125:          /// <summary>
 126:          ///
 127:          /// </summary>
 128:          public static void SendPrivate(string queueName, string label, string text)
 129:          {
 130:              string path;
 131:   
 132:              path = @".\private$\" + queueName;
 133:   
 134:              using (MessageQueue mq = new MessageQueue())
 135:              {
 136:                  try
 137:                  {
 138:                      if (MessageQueue.Exists(path)) mq.Path = path;
 139:                      else MessageQueue.Create(path);
 140:   
 141:                      mq.Send(text, label);
 142:                  }
 143:                  catch (MessageQueueException mqe)
 144:                  {
 145:                  }
 146:              }
 147:          }
 148:   
 149:          ////////////////////////////////////////////////////////////////////////////
 150:   
 151:          /// <summary>
 152:          ///
 153:          /// </summary>
 154:          public static void RecievePrivate(string queueName, out string label, out string body)
 155:          {
 156:              System.Messaging.Message m;
 157:   
 158:              m = RecievePrivateMessage(queueName);
 159:   
 160:              if(m != null)
 161:              {
 162:                  label = m.Label;
 163:                  body = (string)m.Body;
 164:              }
 165:              else
 166:              {
 167:                  label = body = string.Empty;
 168:              }
 169:          }
 170:   
 171:          ////////////////////////////////////////////////////////////////////////////
 172:   
 173:          /// <summary>
 174:          ///
 175:          /// </summary>
 176:          public static System.Messaging.Message RecievePrivateMessage(string queueName)
 177:          {
 178:              // Receive will hang if there were not elements in quere, therefore I have to check if count is > 0 first
 179:              string path;
 180:              System.Messaging.Message m;
 181:   
 182:              path = @".\private$\" + queueName;
 183:   
 184:              using (MessageQueue mq = new MessageQueue())
 185:              {
 186:                  try
 187:                  {
 188:                      if (MessageQueue.Exists(path))
 189:                      {
 190:                          if (Count(queueName) > 0)
 191:                          {
 192:                              mq.Path = path;
 193:   
 194:                              mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
 195:   
 196:                              m = mq.Receive();
 197:                          }
 198:                          else m = null;
 199:                      }
 200:                      else
 201:                      {
 202:                          m = null;
 203:                      }
 204:                  }
 205:                  catch (MessageQueueException mqe)
 206:                  {
 207:                      m = null;
 208:                  }
 209:              }
 210:   
 211:              return m;
 212:          }
 213:   
 214:          ////////////////////////////////////////////////////////////////////////////
 215:   
 216:          /// <summary>
 217:          ///
 218:          /// </summary>
 219:          public static System.Messaging.Message[] RecievePrivateMessageList(string queueName)
 220:          {
 221:              return RecieveOrPeekPrivateMessageList(queueName, true);
 222:          }
 223:   
 224:          ////////////////////////////////////////////////////////////////////////////
 225:   
 226:          /// <summary>
 227:          ///
 228:          /// </summary>
 229:          public static List<string> RecievePrivateList(string queueName)
 230:          {
 231:              System.Messaging.Message[] messageList;
 232:              List<string> list;
 233:   
 234:              messageList = RecieveOrPeekPrivateMessageList(queueName, true);
 235:              list = new List<string>();
 236:   
 237:              if (messageList != null && messageList.Length > 0)
 238:              {
 239:                  foreach (var m in messageList)
 240:                  {
 241:                      list.Add(m.Body.ToString());
 242:                  }
 243:              }
 244:              else
 245:              {
 246:   
 247:              }
 248:   
 249:              return list;
 250:          }
 251:   
 252:          ////////////////////////////////////////////////////////////////////////////
 253:   
 254:          /// <summary>
 255:          ///
 256:          /// </summary>
 257:          private static System.Messaging.Message[] RecieveOrPeekPrivateMessageList(string queueName, bool purgeList)
 258:          {
 259:              string path;
 260:              System.Messaging.Message[] list;
 261:   
 262:              path = @".\private$\" + queueName;
 263:   
 264:              using (MessageQueue mq = new MessageQueue())
 265:              {
 266:                  try
 267:                  {
 268:                      if (MessageQueue.Exists(path))
 269:                      {
 270:                          mq.Path = path;
 271:   
 272:                          mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
 273:   
 274:                          list = mq.GetAllMessages();
 275:   
 276:                          if (purgeList) mq.Purge();
 277:                      }
 278:                      else
 279:                      {
 280:                          list = null;
 281:                      }
 282:                  }
 283:                  catch (MessageQueueException mqe)
 284:                  {
 285:                      list = null;
 286:                  }
 287:              }
 288:   
 289:              return list;
 290:          }
 291:   
 292:          ////////////////////////////////////////////////////////////////////////////
 293:   
 294:          /// <summary>
 295:          ///
 296:          /// </summary>
 297:          public static void Purge(string queueName)
 298:          {
 299:              string path;
 300:   
 301:              path = @".\private$\" + queueName;
 302:   
 303:              using (MessageQueue mq = new MessageQueue())
 304:              {
 305:                  try
 306:                  {
 307:                      if (MessageQueue.Exists(path))
 308:                      {
 309:                          mq.Path = path;
 310:   
 311:                          mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
 312:   
 313:                          mq.Purge();
 314:                      }
 315:                      else
 316:                      {
 317:                      }
 318:                  }
 319:                  catch (MessageQueueException mqe)
 320:                  {
 321:                  }
 322:              }
 323:          }
 324:   
 325:          ////////////////////////////////////////////////////////////////////////////
 326:   
 327:          /// <summary>
 328:          ///
 329:          /// </summary>
 330:          public static void PeekPrivate(string queueName, out string label, out string body)
 331:          {
 332:              System.Messaging.Message m;
 333:   
 334:              m = PeekPrivateMessage(queueName);
 335:   
 336:              if (m != null)
 337:              {
 338:                  label = m.Label;
 339:                  body = (string)m.Body;
 340:              }
 341:              else
 342:              {
 343:                  label = body = string.Empty;
 344:              }
 345:          }
 346:   
 347:          ////////////////////////////////////////////////////////////////////////////
 348:   
 349:          /// <summary>
 350:          ///
 351:          /// </summary>
 352:          public static System.Messaging.Message PeekPrivateMessage(string queueName)
 353:          {
 354:              // Peek will hang if there were not elements in quere, therefore I have to check if count is > 0 first
 355:              string path;
 356:              System.Messaging.Message m;
 357:   
 358:              path = @".\private$\" + queueName;
 359:   
 360:              using (MessageQueue mq = new MessageQueue())
 361:              {
 362:                  try
 363:                  {
 364:                      if (MessageQueue.Exists(path))
 365:                      {
 366:                          if (Count(queueName) > 0)
 367:                          {
 368:                              mq.Path = path;
 369:   
 370:                              mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
 371:   
 372:                              m = mq.Peek();
 373:                          }
 374:                          else m = null;
 375:                      }
 376:                      else
 377:                      {
 378:                          m = null;
 379:                      }
 380:                  }
 381:                  catch (MessageQueueException mqe)
 382:                  {
 383:                      m = null;
 384:                  }
 385:              }
 386:   
 387:              return m;
 388:          }
 389:   
 390:          ////////////////////////////////////////////////////////////////////////////
 391:   
 392:          /// <summary>
 393:          ///
 394:          /// </summary>
 395:          public static System.Messaging.Message[] PeekPrivateMessageList(string queueName)
 396:          {
 397:              return RecieveOrPeekPrivateMessageList(queueName, false);
 398:          }
 399:   
 400:          ////////////////////////////////////////////////////////////////////////////
 401:   
 402:          /// <summary>
 403:          ///
 404:          /// </summary>
 405:          public static List<string> PeekPrivateList(string queueName)
 406:          {
 407:              System.Messaging.Message[] messageList;
 408:              List<string> list;
 409:   
 410:              messageList = RecieveOrPeekPrivateMessageList(queueName, false);
 411:              list = new List<string>();
 412:   
 413:              if(messageList != null && messageList.Length > 0)
 414:              {
 415:                  foreach(var m in messageList)
 416:                  {
 417:                      list.Add(m.Body.ToString());
 418:                  }
 419:              }
 420:              else
 421:              {
 422:   
 423:              }
 424:   
 425:              return list;
 426:          }
 427:   
 428:          ////////////////////////////////////////////////////////////////////////////
 429:   
 430:          /// <summary>
 431:          ///
 432:          /// </summary>
 433:          public static long Count(string queueName)
 434:          {
 435:              long count;
 436:              string path;
 437:              //System.Messaging.Message[] list;
 438:   
 439:              path = @".\private$\" + queueName;
 440:   
 441:              using (MessageQueue mq = new MessageQueue())
 442:              {
 443:                  try
 444:                  {
 445:                      count = 0;
 446:   
 447:                      if (MessageQueue.Exists(path))
 448:                      {
 449:                          mq.Path = path;
 450:   
 451:                          var me = mq.GetMessageEnumerator2();
 452:   
 453:                          while (me.MoveNext()) count++;
 454:                      }
 455:                      else
 456:                      {
 457:                      }
 458:                  }
 459:                  catch (MessageQueueException mqe)
 460:                  {
 461:                      count = 0;
 462:                  }
 463:              }
 464:   
 465:              return count;
 466:          }
 467:   
 468:          ////////////////////////////////////////////////////////////////////////////
 469:   
 470:          /// <summary>
 471:          /// References queues by label.
 472:          /// </summary>
 473:          [Obsolete("Code does not work property and/or is not tested. ")]
 474:          public static void SendByLabel(string queueName)
 475:          {
 476:              using (MessageQueue mq = new MessageQueue("Label:TheLabel"))
 477:              {
 478:                  mq.Send("Queue by label.");
 479:              }
 480:   
 481:              return;
 482:          }
 483:   
 484:          ////////////////////////////////////////////////////////////////////////////
 485:   
 486:          /// <summary>
 487:          /// References queues by format name.
 488:          /// </summary>
 489:          [Obsolete("Code does not work property and/or is not tested. ")]
 490:          public static void SendByFormatName(string queueName)
 491:          {
 492:              using (MessageQueue mq = new MessageQueue("FormatName:Public=5A5F7535-AE9A-41d4-935C-845C2AFF7112"))
 493:              {
 494:                  mq.Send("Queue by format name.");
 495:              }
 496:   
 497:              return;
 498:          }
 499:   
 500:          ////////////////////////////////////////////////////////////////////////////
 501:   
 502:          /// <summary>
 503:          /// References computer journal queues.
 504:          /// </summary>
 505:          [Obsolete("Code does not work property and/or is not tested. ")]
 506:          public static void MonitorComputerJournal()
 507:          {
 508:              using (MessageQueue mq = new MessageQueue(".\\Journal$"))
 509:              {
 510:                  while (true)
 511:                  {
 512:                      Message journalMessage = mq.Receive();
 513:                      // Process the journal message.
 514:                  }
 515:              }
 516:          }
 517:   
 518:          ////////////////////////////////////////////////////////////////////////////
 519:   
 520:          /// <summary>
 521:          /// References queue journal queues.
 522:          /// </summary>
 523:          [Obsolete("Code does not work property and/or is not tested. ")]
 524:          public static void MonitorQueueJournal()
 525:          {
 526:              using (MessageQueue mq = new MessageQueue(".\\myQueue\\Journal$"))
 527:              {
 528:                  while (true)
 529:                  {
 530:                      Message journalMessage = mq.Receive();
 531:                      // Process the journal message.
 532:                  }
 533:              }
 534:          }
 535:   
 536:          ////////////////////////////////////////////////////////////////////////////
 537:   
 538:          /// <summary>
 539:          /// References dead-letter queues.
 540:          /// </summary>
 541:          [Obsolete("Code does not work property and/or is not tested. ")]
 542:          public static void MonitorDeadLetter()
 543:          {
 544:              using (MessageQueue mq = new MessageQueue(".\\DeadLetter$"))
 545:              {
 546:                  while (true)
 547:                  {
 548:                      Message deadMessage = mq.Receive();
 549:                      // Process the dead-letter message.
 550:                  }
 551:              }
 552:          }
 553:   
 554:          ////////////////////////////////////////////////////////////////////////////
 555:   
 556:          /// <summary>
 557:          /// References transactional dead-letter queues.
 558:          /// </summary>
 559:          [Obsolete("Code does not work property and/or is not tested. ")]
 560:          public static void MonitorTransactionalDeadLetter()
 561:          {
 562:              using (MessageQueue mq = new MessageQueue(".\\XactDeadLetter$"))
 563:              {
 564:                  while (true)
 565:                  {
 566:                      Message txDeadLetter = mq.Receive();
 567:                      // Process the transactional dead-letter message.
 568:                  }
 569:              }
 570:          }
 571:   
 572:          ////////////////////////////////////////////////////////////////////////////
 573:          ////////////////////////////////////////////////////////////////////////////
 574:      }
 575:   
 576:      ////////////////////////////////////////////////////////////////////////////
 577:      ////////////////////////////////////////////////////////////////////////////
 578:  }
 579: