Browse DevX
Sign up for e-mail newsletters from DevX


MSMQ for .NET Developers (Part 1) : Page 2

MSMQ provides robust and easy-to-use messaging capabilities that let you send and receive messages between applications running on one machine, different machines, and even across the Internet, both synchronously and asynchronously. Unfortunately, MSMQ is notoriously underused. Find out how to get started with MSMQ and what you can do with it.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Referencing Queues
To send a message to a queue, the sending application must first provide the queue location. MSMQ provides several different methods for identifying queues; the most commonly used queue references are the path name and the direct format name. You commonly use path names to reference public queues, and local private queues. Because they query AD for queue information, you can't use path names to reference private queues located on remote machines. Instead, developers commonly use direct format names to access remote private queues. Direct format names provide a means of interacting with queues directly, avoiding the need for MSMQ to query AD. This fact makes using direct format names the preferred method of accessing queues of all types, because not only does the queuing infrastructure become more robust, but message throughput increases considerably. Both pathnames and direct format names use similar formats. The first portion of the reference specifies the location of the queue by either specifying the machine name or the network address of the server hosting the queue. The second portion of the reference is the queue name, which can include an optional attribute to specify that the queue is private. Table 1 shows several examples of queue references.

Table 1. Sample pathnames and direct format names for both public and private queues.

Path Names
.\DevXTestQueue References a public queue named DevXTestQueue on the local machine.
server01\DevXTestQueue References a public queue named DevXTestQueue on a machine named server01 (local queues can be referenced with a machine name as well).
.\private$\DevXTestQueue References a private queue named DevXTestQueue on the local machine.
server01\private$\DevXTestQueue References a private queue named DevXTestQueue on a machine named server01.
Direct Format Names
Formatname:DIRECT=OS:.\DevXTestQueue References a public queue named DevXTestQueue on the local machine.
Formatname:DIRECT=OS:server01\DevXTestQueue References a public queue named DevXTestQueue on a machine named server01.
Formatname:DIRECT=TCP:\private$\DevXTestQueue References a private queue named DevXTestQueue on the local machine.

Message Formatters
MSMQ under .NET also provides the ability to serialize objects and forward them to other applications. This is really nothing unique, because developers can very easily send objects to other applications using .NET Remoting. But MSMQ provides the ability to deliver those calls asynchronously, in a way that allows the message to survive machine reboots and persist indefinitely. To transfer objects, MSMQ requires you to specify a message formatter to serialize the message. The three main serializers are the ActiveXMessageFormatter, used to serialize simple formats and ActiveX objects; the BinaryMessageFormatter, used to format objects using binary serialization; and the XMLMessageFormatter, which uses XML to serialize messages. Because most messages sent using MSMQ are text strings, you'll probably use the XMLMessageFormatter most often. However, for sending and receiving objects, the binary formatter provides faster messaging with a smaller memory footprint. Formatters are very simple to use; you simply specify which formatter to use when sending a message and which one to use when receiving a message. You'll see examples of how to apply formatters in the downloadable code samples.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date