Working with Message Queues C# - MSMQ

There are situation, where one application has to set some information to queue, and another application wanted to use the data available in queue, you can use the following steps over come the scenarios like this. 
 
Steps to follow:
  1. Create/ Initialize a Queue (where you wanted to push the data).
  2. Add information to Queue(Application 1) (can add any object to Queue, better to use XML string).
  3. Read information from Queue(Application 2). 
Usage of Code 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Messaging;  
  5. using System.Net;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8.   
  9. namespace MessageQueing   
  10. {  
  11.     class Program   
  12.     {  
  13.         static void Main(string[] args)   
  14.       {  
  15.             AddInfoToQueue();  
  16.             ReceiveMessage();  
  17.             Console.ReadLine();  
  18.             Console.ForegroundColor = ConsoleColor.Red;  
  19.             Console.WriteLine("The foreground color is {0}.");  
  20.             Console.ResetColor();  
  21.             Console.WriteLine("The foreground color is {0}.");  
  22.         }  
  23.   
  24.         private static void ColorsDemo()   
  25.       {  
  26.             ConsoleColor[] colors = (ConsoleColor[]) ConsoleColor.GetValues(typeof(ConsoleColor));  
  27.             // Save the current background and foreground colors.  
  28.             ConsoleColor currentBackground = Console.BackgroundColor;  
  29.             ConsoleColor currentForeground = Console.ForegroundColor;  
  30.   
  31.             // Display all foreground colors except the one that matches the background.  
  32.             Console.WriteLine("All the foreground colors except {0}, the background color:",  
  33.                 currentBackground);  
  34.             foreach(var color in colors)   
  35.             {  
  36.                 if (color == currentBackground) continue;  
  37.   
  38.                 Console.ForegroundColor = color;  
  39.                 Console.WriteLine(" The foreground color is {0}.", color);  
  40.             }  
  41.             Console.WriteLine();  
  42.             // Restore the foreground color.  
  43.             Console.ForegroundColor = currentForeground;  
  44.   
  45.             // Display each background color except the one that matches the current foreground color.  
  46.             Console.WriteLine("All the background colors except {0}, the foreground color:",  
  47.                 currentForeground);  
  48.             foreach(var color in colors)   
  49.             {  
  50.                 if (color == currentForeground) continue;  
  51.   
  52.                 Console.BackgroundColor = color;  
  53.                 Console.WriteLine(" The background color is {0}.", color);  
  54.             }  
  55.   
  56.             // Restore the original console colors.  
  57.             Console.ResetColor();  
  58.             Console.WriteLine("\nOriginal colors restored...");  
  59.             Console.ReadLine();  
  60.         }  
  61.         public static void ReceiveMessage()   
  62.       {  
  63.             //Message objMessage = null;  
  64.             MessageQueue m_objMsgQ = null;  
  65.             m_objMsgQ = new MessageQueue(@ ".\Private$\" + "  
  66.                     CSAMQ ");  
  67.                     //objMessage = m_objMsgQ.Peek(new TimeSpan(0,0,2));  
  68.                     //objMessage.AcknowledgeType = AcknowledgeTypes.FullReceive;  
  69.                     //var strRcvXML = objMessage.Body;  
  70.                     System.Messaging.Message[] AllMessages = m_objMsgQ.GetAllMessages();  
  71.                     //Sample 05: Iterate through each message  
  72.                     StringBuilder txtDisplay = new StringBuilder(); foreach(System.Messaging.Message theMessage in AllMessages)   
  73.                 {  
  74.                         //Sample 5.1: Read the Message body as a Byte array  
  75.                         byte[] data = new byte[1024];  
  76.                         theMessage.BodyStream.Read(data, 0, 1024);  
  77.                         string strMessage = ASCIIEncoding.ASCII.GetString(data);  
  78.                         Console.WriteLine(strMessage);  
  79.                     }  
  80.                 }  
  81.                 //As clsEPAcknowledgment  
  82.             public static void AddInfoToQueue()   
  83.       {  
  84.                 string strAdaptormessage = string.Empty;  
  85.                 Message objMessage = null;  
  86.   
  87.                 MessageQueue m_objQInfo = new MessageQueue();  
  88.                 string m_strMessage = null;  
  89.                 MessageQueueTransactionType objMSMQTransactionType = new MessageQueueTransactionType();  
  90.                 try {  
  91.                     objMSMQTransactionType = MessageQueueTransactionType.Single; // TransportType.All;  
  92.   
  93.                     if (CreateQueue() == true)   
  94.                     {  
  95.                         try {  
  96.                             m_strMessage = "<QueueMsg><AdaptorID>123</AdaptorID><LogMsgID>222</LogMsgID><Message>sree</Message></QueueMsg>";  
  97.                             m_objQInfo = new MessageQueue(".\\Private$\\" + "CSAMQ");  
  98.                             //m_objQInfo = New MessageQueue("FormatName:Direct=OS:machinename\\private$\\demoq")  
  99.                             objMessage = new Message();  
  100.                             objMessage.Label = "Message sent at " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");  
  101.                             objMessage.Formatter = new XmlMessageFormatter(new Type[] {  
  102.                                 m_strMessage.GetType()  
  103.                             });  
  104.                             objMessage.Body = m_strMessage;  
  105.                             objMessage.AcknowledgeType = AcknowledgeTypes.FullReachQueue;  
  106.                             m_objQInfo.Send(m_strMessage, objMSMQTransactionType);  
  107.                             m_objQInfo.Close();  
  108.                         } catch (Exception ex)   
  109.                         {  
  110.                             throw new Exception("");  
  111.                         }  
  112.   
  113.                     } else {}  
  114.   
  115.   
  116.                 } catch (Exception ex) {}  
  117.             }  
  118.             public static bool CreateQueue()   
  119.             {  
  120.                 string strLocalIP = string.Empty;  
  121.                 //Variable to hold the IPAddress'  
  122.                 //bool blnStatus = false;  
  123.                 string strSubMethodName = string.Empty;  
  124.                 string m_strRequestPath = string.Empty;  
  125.                 MessageQueue m_objQInfo =  
  126.                     default (MessageQueue);  
  127.   
  128.   
  129.                 //IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName);  
  130.   
  131.                 m_strRequestPath = ".\\Private$\\" + "CSAMQ";  
  132.   
  133.                 if (MessageQueue.Exists(m_strRequestPath))   
  134.                 {  
  135.                     m_objQInfo = new MessageQueue(m_strRequestPath);  
  136.                     m_objQInfo.DefaultPropertiesToSend.Recoverable = true;  
  137.                     return true;  
  138.                     //EPLogMsgDebugInfo("Queue with name " & m_strQName & " already exists.", strSubMethodName, "Check for Queue existence")  
  139.                     //EPLogMessage("Queue with name " & m_strQName & " already exists.", strSubMethodName, "Check for Queue existence", m_objEPEventLog.LogMode, ClsEPEventLog.enEPLogmode.LOGMODE_DEBUG)  
  140.                 } else   
  141.                 {  
  142.                     m_objQInfo = MessageQueue.Create(m_strRequestPath, true);  
  143.                     m_objQInfo.DefaultPropertiesToSend.Recoverable = true;  
  144.                     return true;  
  145.                 }  
  146.                 try   
  147.                 {  
  148.                     //Settitng Security Properties for Queue  
  149.                     m_objQInfo.SetPermissions("Everyone", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);  
  150.                     m_objQInfo.SetPermissions("SYSTEM", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);  
  151.                     m_objQInfo.SetPermissions("ANONYMOUS LOGON", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);  
  152.                 } catch (Exception ex) {  
  153.                     return false;  
  154.                 }  
  155.             }  
  156.   
  157.         }  
  158.     }