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

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