This documentation is archived and is not being maintained.

Specifying the Formatter for Retrieved Messages

You must indicate the formatter object you want to use to retrieve a message from a queue. A formatter indicates how a message will be deserialized when it is removed from the queue. Depending on the type of formatter you use, you might need to set additional properties to specify how the formatter should proceed.

By default, you use the XMLMessageFormatter for most operations. For more information on the formatters available to you, see Message Serialization. For information on the properties unique to the XMLMessageFormatter object, see Introduction to Reading and Retrieving Messages.

You can specify the formatter in code or in the Properties window. In addition, you can either set the formatter on the queue or on the message. You set the formatter on the queue when you plan to access the queue's messages directly, as in the following code:

' Visual Basic 
Console.WriteLine(CStr(MessageQueue1.Receive().Body))

// C#
Console.WriteLine(MessageQueue1.Receive().Body.ToString());

You set the formatter on the message when you plan to retrieve a queue's contents through the message object:

' Visual Basic 
Message1 = MessageQueue1.Receive()

// C#
message1 = messageQueue1.Receive();

To specify the formatter in the Properties window

  1. Create and configure your MessageQueue component instance. For more information, see Creating MessageQueue Component Instances.
  2. In the designer, click on the MessageQueue instance, and then access the Properties window.
  3. Set the Formatter property to the appropriate value.
  4. If you chose either the XMLMessageFormatter object or the BinaryMessageFormatter object, expand the property and set the necessary subproperties to configure your formatter.

To specify the formatter programmatically

  1. Create and configure your MessageQueue component instance. For more information, see Creating MessageQueue Component Instances.
  2. In the Code Editor, create a formatter object of type XMLMessageFormatter, ActiveXMessageFormatter, or BinaryMessageFormatter.
  3. Set any necessary properties for your formatter.
  4. Retrieve your message.

For example, the following code shows how to create and configure a component that uses the XMLMessageFormatter object to retrieve a message from a public queue called MyQueue.

' Visual Basic
Dim mq As New System.Messaging.MessageQueue(".\MyQueue")
Dim formatter As System.Messaging.XmlMessageFormatter = _
   CType(mq.Formatter, System.Messaging.XmlMessageFormatter)
formatter.TargetTypeNames = New String() {"System.String"}
Dim m As System.Messaging.Message = mq.Receive(New TimeSpan(0, 0, 3))


// C#
System.Messaging.MessageQueue mq = 
   new System.Messaging.MessageQueue(".\\MyQueue");
string[] types = { "System.String" };
((System.Messaging.XmlMessageFormatter)mq.Formatter).TargetTypeNames =
   types;
System.Messaging.Message m = mq.Receive(new TimeSpan(0,0,3));

See Also

Receiving Messages Programmatically | Introduction to Reading and Retrieving Messages

Show: