15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1368 ❘ ChaPTer 46 messAGe QueuinG<br />

A message formatter implements the interface<br />

IMessageFormatter. Three message formatters are<br />

available with the namespace System.Messaging:<br />

➤<br />

➤<br />

➤<br />

The XmlMessageFormatter is the default<br />

formatter. It serializes objects using XML. See<br />

Chapter 33, “Manipulating XML,” for more on<br />

XML formatting.<br />

With the BinaryMessageFormatter, messages are<br />

serialized in a binary format. These messages are<br />

shorter than the messages formatted using XML.<br />

The ActiveXMessageFormatter is a binary<br />

formatter so that messages can be read or written<br />

with COM objects. Using this formatter, it is<br />

possible to write a message to the queue with a<br />

.<strong>NET</strong> class <strong>and</strong> to read the message from the queue<br />

with a COM object or vice versa.<br />

The sample message shown in Figure 46-8 with XML is<br />

formatted with the BinaryMessageFormatter in Figure 46-9.<br />

sending Complex Messages<br />

Instead of passing strings, it is possible to pass objects to the Send() method of the MessageQueue class.<br />

The type of the class must fulfill some specific requirements, but they depend on the formatter.<br />

For the binary formatter, the class must be serializable with the [Serializable] attribute. With the<br />

.<strong>NET</strong> runtime serialization, all fields are serialized (this includes private fields). Custom serialization can<br />

be defined by implementing the interface ISerializable. You can read more about the .<strong>NET</strong> runtime<br />

serialization in Chapter 29, “Manipulating Files <strong>and</strong> the Registry.”<br />

XML serialization takes place with the XML formatter. With XML serialization, all public fields<br />

<strong>and</strong> properties are serialized. The XML serialization can be influenced by using attributes from the<br />

System.Xml.Serialization namespace. You can read more about XML serialization in Chapter 33.<br />

receiving messages<br />

To read messages, again, you can use the MessageQueue class. With the Receive() method, a single<br />

message is read <strong>and</strong> removed from the queue. If messages are sent with different priorities, the message<br />

with the highest priority is read. Reading messages with the same priority may mean that the first message<br />

sent is not the first message read because the order of messages across the network is not guaranteed. For a<br />

guaranteed order, you should use transactional message queues.<br />

In the following example, a message is read from the private queue MyPrivateQueue. Previously, a simple<br />

string was passed to the message. When you read a message using the XmlMessageFormatter, you have<br />

to pass the types of the objects that are read to the constructor of the formatter. In the example, the<br />

type System.String is passed to the argument array of the XmlMessageFormatter constructor. This<br />

constructor allows either a String array that contains the types as strings to be passed or a Type array.<br />

The message is read with the Receive() method, <strong>and</strong> then the message body is written to the console:<br />

using System;<br />

using System.Messaging;<br />

namespace Wrox.ProCSharp.Messaging<br />

{<br />

class Program<br />

{<br />

figure 46-9<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!