Message Queuing in the .NET framework.


In this article, we will look into the message queuing in the .net framework. Message queuing supports basic functionalities like establishing connection with the queue, opening, sending message and recieving message from the queue.

Message Queuing in .NET



The System.Message namespace is the basic namespace used in making Message Queuing applications. It supports basic functionalities like establishing connection with the queue, opening, sending message and recieving message from the queue.

Lets analyse the Enqueue and Dequeue the message from the same queue.

Enqueue


Lets write a sample program to analyse Enqueue technique


using System;
using System.Messaging;

public class Enqueue
{
public static void Main( )
{
try
{
string strPath = ".\\PRIVATE$\\NE_queue";
if(!MessageQueue.Exists(strPath))
{
MessageQueue.Create(strPath);
}

MessageQueue queue = new MessageQueue(strPath);

string strQueue = "Queue Me"

queue.Send(c);
}
catch(Exception e)
{

}
}
}


Now when you analyse this code under Computer Management console, you will see the private NE_Queue created with the message we just saved in the queue.

Dequeue


After making sure that the NE_queue path exists in the computer management console, we tell the message queue object to invoke the Recieve() method to dequeue the queuing object in the console.


using System;
using System.Messaging;
using System.Runtime.Serialization;

public class Dequeue
{
public static void Main( )
{
try
{
string strQueue = ".\\PRIVATE$\\NE_queue";

if (!MessageQueue.Exists(strQueue))
{
throw new Exception(strQueue + " doesn't exist!");
}
MessageQueue queue = new MessageQueue(strQueue);

string[] types = {"String, dequeue"};
((XmlMessageFormatter)queue.Formatter).TargetTypeNames = types;

Message msg = queue.Receive(new TimeSpan(0,0,3));

string str = (string) msg.Body;
Console.WriteLine("The string is: {0}", str);
}
catch(Exception e)
{

}
}
}


Now you can compile and execute this code and check the computer management console and you will notice that the string message is not present there.

Hence, Message queueing is the important feature in .net framework where in we can queue and dequeue objects in the internal queue memory of the system.


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: