1. Read Message From Queue
In the previous article, we looked at writing into the message queue. We wrote separate messages and a class instance to it. In this brief article, we will see how we can get Message from the Microsoft Message Queue (MSMQ).
2. About This Read Message MSMQ Example
In this example, we read message queue through the C# windows Form application. Its screenshot is below:

Reading From MSMQ
We will create this sample using C# Windows Application Project. By clicking the Read Messages, we read messages from the private queue (MSMQ) Q1 and display that in the text box shown. One can also use formatters while reading and writing the messages. The formatters are a separate topic to dig into. Here, in this example, we will concentrate on reading messages.
3. Code Explanation
3.1 Assembly Namespace to MSMQ
As told in the previous article on dot net remoting, first we add a reference to the MSMQ assembly (System. Messaging). Once we hold the reference in hand, we can use it in our C# code files. So first we provided the using statement in the form’s ‘.cs’ file.
1 2 |
//Sample 01: To Use Message Queue using System.Messaging; |
3.2 Queue Name With Path
Here, we declare a class level variable
QueueName
. It is holding the Queue with a path to it. This Messaging API will refer to this queue when the user clicks the Read Messages button. Note the ‘.’ notation specifies the root of the queue structure in MSMQ message tree.
1 2 |
//Sample 02: Message queue to read string QueueName = ".\\Private$\\Q1"; |
3.3 Queue Reference
Next, we supply the queue name to the
MessageQueue
constructor. Note we formed this queue name along with the path in the previous section. The variable Q1 is holding reference for this Message Queue which we wrote in the Previous article: Writing to Message Queue Q1. We will use this reference Q1 to call the MSMQ API functions.
1 2 3 |
//Sample 03: Grab the Message Queue Q1 bool NoMessage = true; MessageQueue Q1 = new MessageQueue(QueueName); |
3.4 Reading Messages From Queue
When we execute the Sample discussed in the previous article on dot net remoting, we post four messages in the message queue named Q1.
GetAllMessages
function call of the
MessageQueue
will return the collection all these messages that is not yet read from the queue. In the below code we store all the messages in a
System.Messaging.Message
array.
1 2 |
//Sample 04: Grab the Message Collection from the Queue System.Messaging.Message[] AllMessages = Q1.GetAllMessages(); |
3.5 Read Message Content
Next, we iterate through the
Message
array and read a single message in each iteration from that array. Once we have the message, we are interested in the message content. The
BodyStream
property of the
Message
Instance gives the message content. This body stream is an ‘
IO.Stream
’ instance. As message body is in the form of a stream, we used the
Read
method to retrieve the streaming content in the byte array of 1024 max.
1 2 3 4 5 6 7 |
//Sample 05: Iterate through each message foreach (System.Messaging.Message theMessage in AllMessages) { //Sample 5.1: Read the Message body as a Byte array NoMessage = false; byte[] data = new byte[1024]; theMessage.BodyStream.Read(data, 0, 1024); |
3.6 Display the Message String
To display the message in the text box, we convert the byte array into a text string using the
ASCIIEncoding
. Note, we use
getString
method from
ASCII
class from
ASCIIEncoding
pack. Once we have the body of the message in the form of a string, we display that in the text box. The code is below:
1 2 3 4 5 6 7 |
//Sample 5.2: Convert the byte array to string and display it string strMessage = ASCIIEncoding.ASCII.GetString(data); txtDisplay.AppendText(strMessage); txtDisplay.AppendText(Environment.NewLine); txtDisplay.AppendText( "=================================================="); txtDisplay.AppendText(Environment.NewLine); |
4. Running Our Example
The below video shows the sample in action. Note that the video shows writing into the message queue using the sample created in the early article on dot net remoting and then reading those messages from the example made in this article.
Source Code: Download MSMQ Read Example From Google Drive
- File Processing using CreateFile, ReadFile and WriteFile Win32 APIs
- Dynamically Changing ASP.Net Web Config File
Categories: Remoting
Tags: ASCIIEncoding, BodyStream, MSMQ Reading, System.Messaging.Message