Working with Microsoft Message Queues in C#
page 3 of 4
by Steven Barden
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 23608/ 41

A More Detailed Look At The Methods

[Download Code]
The WriteTextToQueue and WriteDSToQueue methods are functionally similar. In fact with a small amount of code modification they could be combined into a parameterized single method. You will notice that I make constant use of 'out' parameters in functions. It is my preference to make the output of most methods a Boolean indicating success or failure. The functionality of the tree view is fairly straight forward, but there is one point to note.

When the tree is populated, the nodes are listed as the Name (Label) of the Message, and the Tag field is used to denote whether the node is a Queue or a Message. What is not gathered and used in the tree view is the Id of the message. This is very important and after I reached the point where I wanted to use the PeekByID method I found that I had to find the Id of the message I wanted to work with. I considered this and decided that I would reiterate the messages in an individual queue and match the Label with the desired Message Id. You will find that the MessagePeek and RefreshTree methods both contain examples of iterating messages, differing only in the choice of objects used to do so. I suggest that you review each method to find which works best for you.

One point to take particular note of is the Path used to work with a queue. Notice that since I am working with private queues, the Path is in the form of ' .\private$\<queuename> '. Be careful not forget to include the leading '.\' in the string. Depending on how you pull the sting, you may or may not retrieve the leading '.\'. Also, because we are using C#, and you will want to list the characters in this sting as either '.\\private$\\<queuename>' or '@".\private$\<queuename>"'. If you run into problems where the error states that the queue name is not listed in the Directory Service (DS), check to make sure that the queue path is properly escaped and prefixed. You will notice that I use the line 'MessageQueue mq = new MessageQueue( ".\\" + Path ); ' in the MessagePeek method. Upon receiving errors at one point, I noticed that the prefix was missing and decided to correct it in this fashion because I did not want to take the time to track down why it was happening here when it was not happening in other places.  This is never a good practice in production development, but it provided a memorable example of this problem and one way to fix it.

Up until this point I have glossed over most of the functionality, knowing that you can easily find what you need by stepping thru the code, but I will now focus on some of the more interesting details.

As I described earlier, I had to iterate the messages to find the Message Id for the message I wanted to retrieve. You will find that code in the MessagePeek method. Once I found the Id, and used it locate the message I wanted, I had to cast the body of the message in the format that was required to use the data. When you create an object to contain the data in the body of the message, you must first create a message queue formatter. This is used to make sure that if you Send a DataSet, you receive a DataSet. When you create the message formatter, you need to add the object types that the formatter will require to properly assist you in using the contents of the message.

As another side note, while I do not go into detail in this application, the formatter can be either an XML or Binary formatter. This example application uses the XML version of the message queue formatter.


View Entire Article

User Comments

Title: Working with Microsoft Message Queues in C#   
Name: raj
Date: 2011-08-10 11:58:54 PM
Comment:
Web site is very helpful and efficient.
Title: Web site efficiency   
Name: Siphiwe Madi
Date: 2004-07-21 10:18:28 AM
Comment:
I think your Web site is very helpful and efficient.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 4:11:29 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search