Menu ▼

Previous Next

Retrive message from Message Queue :

The following sample code will give you an idea that, how to read messages from Message Queue.


  • Add reference to System.Messaging
  • Add using System.Messaging;
  • private string RetriveMessageFromQueue()
    {
                try
                {
                    string qname = @"FormatName:DIRECT=TCP:192.168.1.3\\Private$\\pvtmsmq";
                    string mqvalue = string.Empty;
                    System.Messaging.MessageQueue mqueu = new System.Messaging.MessageQueue(qname);
                    System.Type[] msgtypes = new System.Type[1];
                    msgtypes[0] = mqvalue.GetType();
                    mqueu.Formatter = new System.Messaging.XmlMessageFormatter(msgtypes);
                    mqvalue = mqueu.Receive(new TimeSpan(0, 0, 5), System.Messaging.MessageQueueTransactionType.Single).Body.ToString();
                    return mqvalue;
                }
                catch
                {
                        throw;
                }        
    }


Previous Next




Search dotnetlearners.com