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

Integrated Applications Programming Company

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