)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Msmq (Ia.Cl.Model)

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