December 2007
Intermediate to advanced
896 pages
19h 57m
English
You want to be able to query for messages with specific criteria from an existing message queue.
Write a query using LINQ to retrieve messages using the System.Messaging.MessageQueue type:
// open an existing message queue
string queuePath = @".\private$\LINQMQ";
MessageQueue messageQueue = new MessageQueue(queuePath);
BinaryMessageFormatter messageFormatter = new BinaryMessageFormatter();
var query = from Message msg in messageQueue
// The first assignment to msg.Formatter is so that we can touch the
// Message object. It assigns the BinaryMessageFormatter to each message
// instance so that it can be read to determine if it matches the criteria.
// Next, a check is performed that the formatter was correctly assigned
// by performing an equality check, which satisfies the Where clause's need
// for a boolean result while still executing the assignment of the formatter.
where ((msg.Formatter = messageFormatter) == messageFormatter) &&
int.Parse(msg.Label) > 5 &&
msg.Body.ToString().Contains('D')
orderby msg.Body.ToString() descending
select msg;
// Check our results for messages with a label > 5 and containing a 'D' in the name
foreach (var msg in query)
{
Console.WriteLine("Label: " + msg.Label + " Body: " + msg.Body);
}The query retrieves the data from the MessageQueue by selecting the messages where the Label is a number greater than 5 and the message body contains a capital letter "D". These messages are then returned sorted by the message ...
Read now
Unlock full access