In the previous article, we looked at writing into the message queue. We wrote separate messages as well as a class instance to it. In this short article, we will see how we can read all the Messages from the Queue.
2) About the sample
Reading message queue is done through the client application and its screenshot is given below:
|Fig 1. Sample Application|
The sample is created using C# Windows Application Project. By clicking the Read Messages, we get all the messages in the private queue Q1 and display that in the text box shown. In the next coming articles, we will see how we can use formatters while reading and writing messages. There is nothing more here to talk about the sample.
3) Source Code
1) As told in the previous article on dot net remoting, a reference to the MSMQ assembly (System. Messaging) is added. Once we have a reference with us, we can start using it in our C# code files. Like that, we provided the using statement in the form’s .cs file.
//Sample 01: To Use Message Queue
2) A class level variable QueueName is declared and it is holding the Queue with a path to it. This Queue will be referred when the Read Messages button clicked by the user.
//Sample 02: Message queue to read
string QueueName = ".\\Private$\\Q1";
3) In the button click handler, we get a reference Q1 to the queue by specifying its name.
//Sample 03: Grab the Message Queue Q1
bool NoMessage = true;
MessageQueue Q1 = new MessageQueue(QueueName);
4) When you execute the Sample discussed in the previous article on dotnet remoting, you get four messages in the message queue named Q1. GetAllMessages function call of the MessageQueue will return the collection all the messaged that is not yet removed from the queue. In the below code we store all the messaged in AllMessages collection.
//Sample 04: Grab the Message Collection from the Queue
System.Messaging.Message AllMessages = Q1.GetAllMessages();
5) Next, we iterate through the collection class to read each individual messages from the collection retrieved. Once we have the message, we are interested in the content of the message. First, we take the BodyStream from the message and this body stream is actually an IO.Stream instance. As message body, we are having is in the form of a stream, we used the Read method to retrieve the streaming content in the byte array of 1024 max.
//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;
theMessage.BodyStream.Read(data, 0, 1024);
6) To display the message in the textbox, the byte array is converted into a text string using the ACCIIEncoding. Once we have the body of the message in the form of string we will display that in the text box. The code for that is shown below.
//Sample 5.2: Convert the byte array to string and display it
string strMessage = ASCIIEncoding.ASCII.GetString(data);
4. Running the Sample
Below given video shows the sample in action. Note that the video shows, writing into the message queue using the sample application created in the previous article on dotnet remoting and then reading those messages from the sample created in this article.
Source Code : Download