Message Serialization
Serialization is the process of taking objects and converting their state information into a form that can be stored or transported. The basic idea of serialization is that an object writes its current state, usually indicated by the value of its member variables, to persistent storage. Later, the object can be re-created by reading, or deserializing, the object's state from the storage. Serialization handles all the details of object pointers and circular object references that are used when you serialize an object.
In the message-queuing feature, serialization refers specifically to the process of converting an object or set of data into a message that can be sent to a queue, and then converting messages retrieved from the queue back into objects or data that your application can process.
A formatter object handles message serialization in your Visual Studio or .NET Framework applications. When a message is sent to the queue, the formatter serializes an object into a stream that can be sent to the message queue. When reading from a queue, the formatter deserializes the message data into the Body property.
You choose the appropriate type of formatter for the kind of data you want to send and receive. Visual Studio and the .NET Framework ship three predefined formatters, each designed to persist and de-persist a different type of data:
The XmlMessageFormatter object persists objects and primitive data types into and out of messages using human-readable XML strings. This is the default formatter setting for MessageQueue components.
The BinaryMessageFormatter object persists one or more connected objects into serialized streams. The result is very compact and fast to parse, but not human-readable.
The ActiveXMessageFormatter object persists primitive data types, allowing for interoperability with components that use previous versions of Message Queuing. The resulting serialization is very compact. This formatter is designed with Windows in mind and does not produce human-readable results. However, it is an extremely fast method of serialization.
By default, an XmlMessageFormatter is created for you when you create a MessageQueue component instance and is associated with the instance. When the Send method is called on the MessageQueue instance, the body of your message is serialized using this formatter. You do not have to write any additional code to use the formatter during a send operation.
Reading a message is slightly more complex. To read from the queue using a formatter, you must set properties indicating how the body of the message should be handled. If you are using the XmlMessageFormatter object, you set the TargetTypes or TargetTypeNames property. If you are using the BinaryMessageFormatter object, you set a different series of properties. The ActiveXMessageFormatter object has no associated properties. For more information, see Reading and Receiving Messages.
In addition to using the formatters shipped with Visual Studio and the .NET Framework, you can create your own formatters if you need to work with different types of data. You can import your formatter into a project and access it in code.
You choose the formatter you want to use by setting the Formatter
property for the queue, message, or MessageQueue component with which you are working.
Advantages of XML Message Formatting
One advantage of the XML formatter is that you can read the strings that are created when the object or data is serialized. This means that if something happens and the message cannot be de-persisted from the message queue, you can take a look at the message itself and likely fix the problem.
Another advantage is that messages serialized with this formatter do not have to be deserialized by the same formatter object. That is, you do not need to have the same formatter class in the receiver as you had in the sender, as long as both parties know the data schema. In addition, messages serialized by the XML formatter do not necessarily have to be deserialized at all. Most browsers and other XML viewers can parse XML messages.
See Also
Tasks
How to: Create MessageQueue Component Instances