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:
- Create/ Initialize a Queue (where you wanted to push the data).
- Add information to Queue(Application 1) (can add any object to Queue, better to use XML string).
- Read information from Queue(Application 2).
Usage of Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Messaging;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace MessageQueing
- {
- class Program
- {
- static void Main(string[] args)
- {
- AddInfoToQueue();
- ReceiveMessage();
- Console.ReadLine();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("The foreground color is {0}.");
- Console.ResetColor();
- Console.WriteLine("The foreground color is {0}.");
- }
-
- private static void ColorsDemo()
- {
- ConsoleColor[] colors = (ConsoleColor[]) ConsoleColor.GetValues(typeof(ConsoleColor));
-
- ConsoleColor currentBackground = Console.BackgroundColor;
- ConsoleColor currentForeground = Console.ForegroundColor;
-
-
- Console.WriteLine("All the foreground colors except {0}, the background color:",
- currentBackground);
- foreach(var color in colors)
- {
- if (color == currentBackground) continue;
-
- Console.ForegroundColor = color;
- Console.WriteLine(" The foreground color is {0}.", color);
- }
- Console.WriteLine();
-
- Console.ForegroundColor = currentForeground;
-
-
- Console.WriteLine("All the background colors except {0}, the foreground color:",
- currentForeground);
- foreach(var color in colors)
- {
- if (color == currentForeground) continue;
-
- Console.BackgroundColor = color;
- Console.WriteLine(" The background color is {0}.", color);
- }
-
-
- Console.ResetColor();
- Console.WriteLine("\nOriginal colors restored...");
- Console.ReadLine();
- }
- public static void ReceiveMessage()
- {
-
- MessageQueue m_objMsgQ = null;
- m_objMsgQ = new MessageQueue(@ ".\Private$\" + "
- CSAMQ ");
-
-
-
- System.Messaging.Message[] AllMessages = m_objMsgQ.GetAllMessages();
-
- StringBuilder txtDisplay = new StringBuilder(); foreach(System.Messaging.Message theMessage in AllMessages)
- {
-
- byte[] data = new byte[1024];
- theMessage.BodyStream.Read(data, 0, 1024);
- string strMessage = ASCIIEncoding.ASCII.GetString(data);
- Console.WriteLine(strMessage);
- }
- }
-
- public static void AddInfoToQueue()
- {
- string strAdaptormessage = string.Empty;
- Message objMessage = null;
-
- MessageQueue m_objQInfo = new MessageQueue();
- string m_strMessage = null;
- MessageQueueTransactionType objMSMQTransactionType = new MessageQueueTransactionType();
- try {
- objMSMQTransactionType = MessageQueueTransactionType.Single;
-
- if (CreateQueue() == true)
- {
- try {
- m_strMessage = "<QueueMsg><AdaptorID>123</AdaptorID><LogMsgID>222</LogMsgID><Message>sree</Message></QueueMsg>";
- m_objQInfo = new MessageQueue(".\\Private$\\" + "CSAMQ");
-
- objMessage = new Message();
- objMessage.Label = "Message sent at " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
- objMessage.Formatter = new XmlMessageFormatter(new Type[] {
- m_strMessage.GetType()
- });
- objMessage.Body = m_strMessage;
- objMessage.AcknowledgeType = AcknowledgeTypes.FullReachQueue;
- m_objQInfo.Send(m_strMessage, objMSMQTransactionType);
- m_objQInfo.Close();
- } catch (Exception ex)
- {
- throw new Exception("");
- }
-
- } else {}
-
-
- } catch (Exception ex) {}
- }
- public static bool CreateQueue()
- {
- string strLocalIP = string.Empty;
-
-
- string strSubMethodName = string.Empty;
- string m_strRequestPath = string.Empty;
- MessageQueue m_objQInfo =
- default (MessageQueue);
-
-
-
-
- m_strRequestPath = ".\\Private$\\" + "CSAMQ";
-
- if (MessageQueue.Exists(m_strRequestPath))
- {
- m_objQInfo = new MessageQueue(m_strRequestPath);
- m_objQInfo.DefaultPropertiesToSend.Recoverable = true;
- return true;
-
-
- } else
- {
- m_objQInfo = MessageQueue.Create(m_strRequestPath, true);
- m_objQInfo.DefaultPropertiesToSend.Recoverable = true;
- return true;
- }
- try
- {
-
- m_objQInfo.SetPermissions("Everyone", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);
- m_objQInfo.SetPermissions("SYSTEM", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);
- m_objQInfo.SetPermissions("ANONYMOUS LOGON", MessageQueueAccessRights.FullControl, AccessControlEntryType.Allow);
- } catch (Exception ex) {
- return false;
- }
- }
-
- }
- }