How to send and receive messages through message queuing in .Net

Message queuing makes it easy for application developers to communicate with application programs quickly and reliably by sending and receiving messages. Messaging provides you with guaranteed message delivery and a robust, fail-safe way to carry out many of your business processes. The MessageQueue component allows you to easily incorporate message-based communication into your applications. Using this component and its associated language features, you can send and receive messages, explor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;

namespace PubishApps
{
    public class MessageQueuing
    {

/************************************************
* Topic : How to send and receive messages through message queuing in .Net
* Reference Required: System.Messaging.
* Author : kalit sikka
* Prerequisite: Microsoft Message Queue component(MSMQ).
  By default, the operating system will not install a message queue service on your machine
  unless you explicitly specify. For the explicit installation, go to the Add/Remove > Window Component Wizard > Message Queue
* For : http://eggheadcafe.com
* **********************************************/

        
         /// <summary>
        /// To Create Message Queue
        /// </summary>
        /// <param name="QueueName"></param>
        /// <param name="label"></param>
        public void CreatingMessageQueue(string QueueName, string label)
        {
             if(!MessageQueue.Exists(@".\'"+QueueName+"'"))
             {
          
                     using (MessageQueue queue = MessageQueue.Create(@".\'"+QueueName+"'"))
                     {
                        queue.Label = label;
                         Console.WriteLine("Queue created:");
                         Console.WriteLine("Path: {0}", queue.Path);
                          Console.WriteLine("FormatName: {0}", queue.FormatName);
                     }
            }

        }

         /// <summary>
        /// To send Message
        /// </summary>
        /// <param name="QueueName"></param>
        /// <param name="MessageTo"></param>
        public void SendingMessage(string QueueName, string MessageTo)
        {
           try
           {
             if (!MessageQueue.Exists(@".\'"+QueueName+"'"))
            {
               MessageQueue.Create(@".\'"+QueueName+"'");
            }
            MessageQueue queue = new MessageQueue(@".\'"+QueueName+"'");
           queue.Formatter = new XmlMessageFormatter();
           queue.Send(MessageTo, "Label");
         }
         catch (MessageQueueException ex)
         {
            Console.WriteLine(ex.Message);
         }

        }

        /// <summary>
        /// To Read Messages from Message queue
        /// </summary>
        /// <param name="QueueName"></param>
        public void ReadMessages(string QueueName)
        {
            MessageQueue queue = new MessageQueue(@".\'"+QueueName+"'");
            queue.Formatter = new XmlMessageFormatter(
                   new string[] {"System.String"});
            foreach (Message message in queue)
            {
               Console.WriteLine(message.Body);
             }

         }
        /// <summary>
        /// Use Enumerator for Reading Message
        /// </summary>
        /// <param name="QueueName"></param>
        public void ReadMessageByEnumerator(string QueueName)
        {
              MessageQueue queue = new MessageQueue(@".\'"+QueueName+"'");
        queue.Formatter = new XmlMessageFormatter(
               new string[] {"System.String"});

        using (MessageEnumerator messages = queue.GetMessageEnumerator())
        {
           while (messages.MoveNext(TimeSpan.FromMinutes(30)))
           {
              Message message = messages.Current;
              Console.WriteLine(message.Body);
           }
        }


        }


         static void Main(string[] args)
        {
            

            MessageQueuing oMessage = new MessageQueuing();
             oMessage.CreatingMessageQueue("MessageSys", "MessageLogging");
            oMessage.SendingMessage("MessageSys", "First Message");
            oMessage.ReadMessages("MessageSys");
            oMessage.ReadMessageByEnumerator("MessageSys");
        }


     }
}
By Kalit Sikka   Popularity  (2212 Views)