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());
Console.WriteLine(MessageQueue1.Receive().get_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();
message1 = messageQueue1.Receive();
To specify the formatter in the Properties window
Create and configure your MessageQueue component instance. For more information, see How to: Create MessageQueue Component Instances.
In the designer, click on the MessageQueue instance, and then access the Properties window.
Set the Formatter property to the appropriate value.
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
Create and configure your MessageQueue component instance. For more information, see How to: Create MessageQueue Component Instances.
In the Code Editor, create a formatter object of type XmlMessageFormatter, ActiveXMessageFormatter, or BinaryMessageFormatter.
Set any necessary properties for your formatter.
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));
System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue(".\\MyQueue"); String[] types = {"System.String"}; ((System.Messaging.XmlMessageFormatter) (mq.get_Formatter())).set_TargetTypeNames(types); System.Messaging.Message m = mq.Receive(new TimeSpan(0, 0, 3));
See Also
Tasks
How to: Receive Messages Programmatically