How to send and receive messages in MSMQ using C#/VB
In this blog, we will discuss on how to implement System.Messaging class to send and receive the XML messages to a MSMQ queue and some issues that you may come across while receiving the XML messages.
We will begin with adding reference to the System.Messaging dll. The path of the dll is:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\
Send Messages to MSMQ Queue
Load XML message content:
XmlDocument xd = new XmlDocument();
xd.Load(@"c:\temp\myxml.xml");
Create a new message using the following code:
System.Messaging.Message msg = new System.Messaging.Message();
Set the message label and message body:
msg.Label = "TestMessage"; msg.Body = xd.InnerXml;
msg.UseDeadLetterQueue = true; // to send the message to the dead letter queue in case if there is some issue while sending.
Create an object of the queue to which you want to send the message:
MessageQueue msgQ = new MessageQueue(".\\private$\\QueueName");
Use send() function to send the message msgQ.Send(msg);
The corresponding code in VB is as follows:
Dim xd As New XmlDocument() xd.Load("c:\temp\myxml.xml")
Dim msg As New System.Messaging.Message()
msg.Label = "TestMessage"
msg.Body = xd.InnerXml
msg.UseDeadLetterQueue = True
Dim msgQ As New MessageQueue(".\private$\QueueName ")
msgQ.Send(msg)
You will see the message in the queue
Note: When we send a message to the MSMQ queue using System.Messaging class, the message body is surrounded by a <String> Wrapper and <?xml version="1.0"?>
Receive Message in MSMQ
Create an object of the message and the queue from which you want to receive the message:
MessageQueue msgQ = new MessageQueue(".\\private$\\QueueName");
System.Messaging.Message m = new System.Messaging.Message();
Use receive() function to receive the message:
m = msgQ.Receive();
Use XMLMessageFormatter to receive the message without the string wrapper.
m.Formatter = new XmlMessageFormatter(new String[] {"System.String,mscorlib"});
string text = m.Body.ToString();
Save the message to the file location to test:
XmlDocument xml = new XmlDocument();
xml.LoadXml(text);
xml.Save(@"c:\temp\myxml.xml");
The corresponding code in VB is as follows:
Dim m As New System.Messaging.Message()
Dim msgQ As New MessageQueue(".\private$\Testpgm")
m = msgQ.Receive()
m.Formatter = New XmlMessageFormatter(New [String]() {"System.String,mscorlib"})
Dim text As String = m.Body.ToString()
Dim xml As New XmlDocument()
xml.LoadXml(text)
xml.Save("c:\temp\myxml.xml")
To receive the message which does not have <string> wrapper:
In order to receive a message which does not have a <sting> wrapper, use ActiveXMessageFormatter() instead of XmlMessageFormatter().
I am attaching a sample solution for reference.
Sample snippets to send\Receive messages to remote MSMQ queues:
//Sending to Public Non-Transactional queue
public void SendPublicNonTx()
{
MessageQueue myQueue = new MessageQueue();
myQueue.Path = "machinename\\testntx";
myQueue.Send("Queue by format name.");
return;
}
//Sending to Public Non-Transactional queue
public void SendPublicNonTx()
{
MessageQueue myQueue = new MessageQueue("FormatName:Public=8FC22E24-C378-4F9A-91FC-550785FC495E");
myQueue.Send("Queue by format name.");
return;
}
//Sending to Private Transactional queue
public void SendPrivateTx()
{
MessageQueue rmQ = new MessageQueue("FormatName:Direct=OS:machinename\\private$\\testq");
rmQ.Send("message", MessageQueueTransactionType.Single);
}
//Sending to Private Non-Transactional queue
public void SendPrivateNonTx()
{
MessageQueue myQueue = new MessageQueue(@"FormatName:Direct=OS:machinename\private$\testNontx");
myQueue.Send("my message");
return;
}
//Sending to Public Transactional queue
public void SendPublicTx()
{
MessageQueue myQueue = new MessageQueue("FormatName:Public= 09BA7806-2FBA-443C-880A-A60D525A0F53"); // we are using the guid to identify the queue.
myQueue.Send("Queue by format name.", MessageQueueTransactionType.Single);
return;
}
//Reading from Remote Public queue:
private void GetFromPublicQueue()
{
MessageQueue mQ = new MessageQueue("FormatName:Public=8FC22E24-C378-4F9A-91FC-550785FC495E");
mQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
System.Messaging.Message msg = mQ.Receive();
MessageBox.Show(msg.Body.ToString());
MessageQueue rmTxQ = new MessageQueue("FormatName:Public= 09BA7806-2FBA-443C-880A-A60D525A0F53");
mTxQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
System.Messaging.Message msgTx = mTxQ.Receive(MessageQueueTransactionType.Single);
MessageBox.Show(msgTx.Body.ToString());
//Reading from Remote Private queue:
private void GetFromQueue()
{
MessageQueue mQ = new MessageQueue("FormatName:Direct=OS:machinename\\private$\\testNontx");
mQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
System.Messaging.Message msg = mQ.Receive();
MessageBox.Show(msg.Body.ToString());
MessageQueue mTxQ = new MessageQueue("FormatName:Direct=OS:machinename\\private$\\testq");
mTxQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
System.Messaging.Message msgTx = mTxQ.Receive(MessageQueueTransactionType.Single);
MessageBox.Show(msgTx.Body.ToString());
MessageQueue mTxQ = new MessageQueue("FormatName:Direct=OS:machinename\\private$\\testq");
mTxQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
System.Messaging.Message msgTx = mTxQ.Receive(MessageQueueTransactionType.Single);
MessageBox.Show(msgTx.Body.ToString());
Hope this helps !!!
Written By
Rasika Chaudhary, Jainath V R
Reviewed By
Jainath V R
Microsoft India GTSC.
Comments
Anonymous
June 10, 2015
This is very good article. Thanks Rasika and JainathAnonymous
September 22, 2015
Does message queue have to installed on every machine?Anonymous
December 28, 2015
Is there a way how to specify your own root element and not to have the <string> there?