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));
- // Save the current background and foreground colors.
- ConsoleColor currentBackground = Console.BackgroundColor;
- ConsoleColor currentForeground = Console.ForegroundColor;
- // Display all foreground colors except the one that matches the background.
- 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();
- // Restore the foreground color.
- Console.ForegroundColor = currentForeground;
- // Display each background color except the one that matches the current foreground color.
- 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);
- }
- // Restore the original console colors.
- Console.ResetColor();
- Console.WriteLine("\nOriginal colors restored...");
- Console.ReadLine();
- }
- public static void ReceiveMessage()
- {
- //Message objMessage = null;
- MessageQueue m_objMsgQ = null;
- m_objMsgQ = new MessageQueue(@ ".\Private$\" + "
- CSAMQ ");
- //objMessage = m_objMsgQ.Peek(new TimeSpan(0,0,2));
- //objMessage.AcknowledgeType = AcknowledgeTypes.FullReceive;
- //var strRcvXML = objMessage.Body;
- System.Messaging.Message[] AllMessages = m_objMsgQ.GetAllMessages();
- //Sample 05: Iterate through each message
- StringBuilder txtDisplay = new StringBuilder(); foreach(System.Messaging.Message theMessage in AllMessages)
- {
- //Sample 5.1: Read the Message body as a Byte array
- byte[] data = new byte[1024];
- theMessage.BodyStream.Read(data, 0, 1024);
- string strMessage = ASCIIEncoding.ASCII.GetString(data);
- Console.WriteLine(strMessage);
- }
- }
- //As clsEPAcknowledgment
- 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; // TransportType.All;
- if (CreateQueue() == true)
- {
- try {
- m_strMessage = "<QueueMsg><AdaptorID>123</AdaptorID><LogMsgID>222</LogMsgID><Message>sree</Message></QueueMsg>";
- m_objQInfo = new MessageQueue(".\\Private$\\" + "CSAMQ");
- //m_objQInfo = New MessageQueue("FormatName:Direct=OS:machinename\\private$\\demoq")
- 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;
- //Variable to hold the IPAddress'
- //bool blnStatus = false;
- string strSubMethodName = string.Empty;
- string m_strRequestPath = string.Empty;
- MessageQueue m_objQInfo =
- default (MessageQueue);
- //IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName);
- m_strRequestPath = ".\\Private$\\" + "CSAMQ";
- if (MessageQueue.Exists(m_strRequestPath))
- {
- m_objQInfo = new MessageQueue(m_strRequestPath);
- m_objQInfo.DefaultPropertiesToSend.Recoverable = true;
- return true;
- //EPLogMsgDebugInfo("Queue with name " & m_strQName & " already exists.", strSubMethodName, "Check for Queue existence")
- //EPLogMessage("Queue with name " & m_strQName & " already exists.", strSubMethodName, "Check for Queue existence", m_objEPEventLog.LogMode, ClsEPEventLog.enEPLogmode.LOGMODE_DEBUG)
- } else
- {
- m_objQInfo = MessageQueue.Create(m_strRequestPath, true);
- m_objQInfo.DefaultPropertiesToSend.Recoverable = true;
- return true;
- }
- try
- {
- //Settitng Security Properties for Queue
- 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;
- }
- }
- }
- }

Pavan BujjiPosted Oct 25, 2017, 9:23 AM
Nice article..i have few doubts on it.how to insert the records in Dynamics GP from Message Queue.what are the prerequisites to use Message Queue ?.I created private Queues but i didn't get any record to insert in GP.how can i ?.
Santhakumar MunuswamyPosted Dec 1, 2015, 3:18 AM
Nice share