Part #2 Structure
Part #3 Code
Coding structure concept:
- Initialize the queue
- Begin listening for new events on the queue OR Send a message to the queue
- Close the queue
MessageQueue
The MSMQ message queue that will receive/send messages.
Message
The actual message or data sent to or read from a queue.
Interacting with Message Queues:
http://msdn.microsoft.com/en-us/library/ms973860.aspx
#open
Queue Path
Two basic types of queues exist: public and private. While there are slight security differences between using public and private queues, they are for the most part, the same in usage. In code, they only differ by the path you use to connect to the queue. Specifically, two paths are shown below to connect to a public and private queue. The text "Private$" precedes the path for private message queues.
Public Message Queue Path
.\\MyQueue
Private Message Queue Path
.\\Private$\\MyQueue
For more info on Queue Paths see:
http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.path.aspx
Note: BinaryMessageFormatter() allows you to serialize any type of object in the queue's message body and deserialize it back to its original object type.
Note: In a disconnected environment, where the queue cannot be reached at the time the message is sent, it is necessary to use the format name.
Format Name Reference
A format name is a unique identifier for a queue that is generated by MSMQ when the queue is created. The format name can also be generated later by the application. A format name reference is in the form of a string indicating whether a queue is public or private, followed by the GUID for the queue, and other identifiers as necessary.
Note: This is the most efficient way to reference a queue, since MSMQ doesn't have to interpret paths or labels to find the queue.
Path information:
Queue Type | Syntax |
---|---|
Public queue | FORMATNAME:PUBLIC=QueueGUID |
Private queue | FORMATNAME:PRIVATE= MachineGUID\QueueNumber |
Journal queue | FORMATNAME:PUBLIC= QueueGUID;JOURNAL or FORMATNAME:PRIVATE= MachineGUID\QueueNumber;JOURNAL |
e.g.
@"FormatName:DIRECT=TCP:65.55.57.27\private$\test"
Note: Direct format names that specify the HTTP or HTTPS protocol cannot be used to peek at or receive messages, only to send them.
AppSpecific property
The AppSpecific property is an integer unused by Windows and is available to the developer.
This property is often used by developers in the case of messages possibly failing and being re-tried, in which case it is used to count how many times the message has been sent. After the value reaches a certain threshold, the message may be posted to the Dead-letter messages queue located under the "System Queues" folder in Computer Management.
#retrieving
Receiving Messages from the Queue
Receiving messages generally happens asynchronously in Microsoft Message Queue MSMQ. You add a ReceiveCompleted event to the queue and begin listening for new messages to arrive. When a message arrives, your event is triggered, passed a message object, and you can then deserialize the body and process the contents. Your final call in the ReceiveCompleted function would be to start listening again for new messages
Peek()
Returns without removing (peeks) the first message in the queue referenced by this MessageQueue.
Note: The Peek method is synchronous, so it blocks the current thread until a message becomes available or the specified time-out occurs.
Receive()
With the Receive() method, a single message is read and removed from the queue.
Receive() message behaves synchronously and waits until a message is in the queue if there is none.
The Message Loop
"queue.BeginReceive()". This is a critical line to a successful implementation of Microsoft Message Queue in that it provides the means for continuous listening on the message queue. Each time a message is received, the listening process stops. This helps provide a thread-safe environment. However, it also means it's the developer's responsibility to resume listening to the queue.
#closing
Note: Since the message queue object implements the IDisposable interface, we can utilize the "using" statement to automatically close and dispose of the queue for us.
using (queue = new MessageQueue(".\\MyQueue"))
Official Microsoft Code Samples:
How to write to and read from Microsoft Message Queuing in Visual C# - http://support.microsoft.com/kb/815811
Sending Messages to Multiple Destination Examples - http://msdn.microsoft.com/en-us/library/windows/desktop/ms701536(v=vs.85).aspx
The content of this series was found merged and customised from the following sites:
http://support.microsoft.com/kb/178517
http://support.microsoft.com/kb/815811
http://technet.microsoft.com/en-us/library/cc730994
http://www.primaryobjects.com/CMS/Article77.aspx
http://en.wikipedia.org/wiki/Microsoft_Message_Queuing
http://www.c-sharpcorner.com/UploadFile/rajkpt/101262007012217AM/1.aspx
http://www.techrepublic.com/article/using-message-queue-services-in-net/6136686
http://technet.microsoft.com/en-us/library/154e24ed-e149-4a2b-85cc-0dbae721cf48
http://msdn.microsoft.com/en-us/library/system.messaging.message.messagetype.aspx