Share via


How to: Specify 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:

Console.WriteLine(CStr(MessageQueue1.Receive().Body))
      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:

Message1 = MessageQueue1.Receive()
      message1 = messageQueue1.Receive();

To specify the formatter in the Properties window

  1. Create and configure your MessageQueue component instance. For more information, see How to: Create 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 How to: Create 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.

    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))
    
            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

Tasks

How to: Receive Messages Programmatically

Concepts

Introduction to Reading and Retrieving Messages