MessageQueue.Formatter Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets the formatter used to serialize an object into or deserialize an object from the body of a message read from or written to the queue.

Namespace:   System.Messaging
Assembly:  System.Messaging (in System.Messaging.dll)

[BrowsableAttribute(false)]
[MessagingDescriptionAttribute("MQ_Formatter")]
public IMessageFormatter Formatter { get; set; }

Property Value

Type: System.Messaging.IMessageFormatter

The IMessageFormatter that produces a stream to be written to or read from the message body. The default is XmlMessageFormatter.

The Formatter property contains an instance of a formatter object, which transforms messages when your application reads or writes to the queue.

When the application sends message to the queue, the formatter serializes the object into a stream and inserts it into the message body. When reading from a queue, the formatter deserializes the message data into the Body property of a Message.

The XmlMessageFormatter is loosely coupled, so it is not necessary to have the same object type on the sender and receiver when using this format. The ActiveXMessageFormatter and BinaryMessageFormatter serialize the data into binary representation. The ActiveXMessageFormatter is used when sending or receiving COM components.

BinaryMessageFormatter and ActiveXMessageFormatter provide faster throughput than the XmlMessageFormatter. The ActiveXMessageFormatter allows interoperability with Visual Basic 6.0 Message Queuing applications.

When your application sends messages to the queue, the MessageQueue.Formatter applies only to those messages that use the default message properties, DefaultPropertiesToSend. If you send a Message to the queue, Message Queuing uses the formatter defined in the Message.Formatter property to serialize the body instead.

The MessageQueue class will always use a Message to receive or peek a message from the queue. The message is deserialized using the MessageQueue.Formatter property.

The following table shows whether this property is available in various Workgroup modes.

Workgroup mode

Available

Local computer

Yes

Local computer and direct format name

Yes

Remote computer

No

Remote computer and direct format name

Yes

The following code example demonstrates formatting a message body using BinaryMessageFormatter.

using System;
using System.Messaging;
using System.Drawing;
using System.IO;

namespace MyProject
{


	/// <summary>
	/// Provides a container class for the example.
	/// </summary>
	public class MyNewQueue
	{

		//**************************************************
		// Provides an entry point into the application.
		//		 
		// This example sends and receives a message from
		// a queue.
		//**************************************************

		public static void Main()
		{
			// Create a new instance of the class.
			MyNewQueue myNewQueue = new MyNewQueue();

			// Create a queue on the local computer.
			CreateQueue(".\\myQueue");

			// Send a message to a queue.
			myNewQueue.SendMessage();

			// Receive a message from a queue.
			myNewQueue.ReceiveMessage();

			return;
		}

		//**************************************************
		// Creates a new queue.
		//**************************************************

		public static void CreateQueue(string queuePath)
		{
			try	
				{
				if(!MessageQueue.Exists(queuePath))
				{
					MessageQueue.Create(queuePath);
				}
				else
				{
					Console.WriteLine(queuePath + " already exists.");
				}
			}
			catch (MessageQueueException e)
			{
				Console.WriteLine(e.Message);
			}

		}

		//**************************************************
		// Sends an image to a queue, using the BinaryMessageFormatter.
		//**************************************************

		public void SendMessage()
		{
			try{

				// Create a new bitmap.
				// The file must be in the \bin\debug or \bin\retail folder, or
				// you must give a full path to its location.
				Image myImage = Bitmap.FromFile("SentImage.bmp");

				// Connect to a queue on the local computer.
				MessageQueue myQueue = new MessageQueue(".\\myQueue");

				Message myMessage = new Message(myImage, new BinaryMessageFormatter());

				// Send the image to the queue.
				myQueue.Send(myMessage);
			}
			catch(ArgumentException e)
			{
				Console.WriteLine(e.Message);

			}

			return;
		}


		//**************************************************
		// Receives a message containing an image.
		//**************************************************

		public  void ReceiveMessage()
		{

			try
			{

				// Connect to the a queue on the local computer.
				MessageQueue myQueue = new MessageQueue(".\\myQueue");

				// Set the formatter to indicate body contains an Order.
				myQueue.Formatter = new BinaryMessageFormatter();

				// Receive and format the message. 
				System.Messaging.Message myMessage = myQueue.Receive(); 
				Bitmap myImage = (Bitmap)myMessage.Body;

				// This will be saved in the \bin\debug or \bin\retail folder.
				myImage.Save("ReceivedImage.bmp",System.Drawing.Imaging.ImageFormat.Bmp);


			}

			catch (MessageQueueException)
			{
				// Handle Message Queuing exceptions.
			}

			// Handle invalid serialization format.
			catch (InvalidOperationException e)
			{
				Console.WriteLine(e.Message);
			}

			catch (IOException e)
			{
				// Handle file access exceptions.
			}

			// Catch other exceptions as necessary.

			return;
		}
	}
}

The following code example demonstrates formatting a message body using XmlMessageFormatter.

using System;
using System.Messaging;
using System.Drawing;
using System.IO;

namespace MyProject
{

	// The following example 
	// sends to a queue and receives from a queue.
	public class Order
	{
		public int orderId;
		public DateTime orderTime;
	};	

	/// <summary>
	/// Provides a container class for the example.
	/// </summary>
	public class MyNewQueue
	{

		//**************************************************
		// Provides an entry point into the application.
		//		 
		// This example sends and receives a message from
		// a queue.
		//**************************************************

		public static void Main()
		{
			// Create a new instance of the class.
			MyNewQueue myNewQueue = new MyNewQueue();

			// Create a queue on the local computer.
			CreateQueue(".\\myQueue");

			// Send a message to a queue.
			myNewQueue.SendMessage();

			// Receive a message from a queue.
			myNewQueue.ReceiveMessage();

			return;
		}

		//**************************************************
		// Creates a new queue.
		//**************************************************

		public static void CreateQueue(string queuePath)
		{
			try	
			{
				if(!MessageQueue.Exists(queuePath))
				{
					MessageQueue.Create(queuePath);
				}
				else
				{
					Console.WriteLine(queuePath + " already exists.");
				}
			}
			catch (MessageQueueException e)
			{
				Console.WriteLine(e.Message);
			}

		}

		//**************************************************
		// Sends an Order to a queue.
		//**************************************************

		public void SendMessage()
		{
			try
			{

				// Create a new order and set values.
				Order sentOrder = new Order();
				sentOrder.orderId = 3;
				sentOrder.orderTime = DateTime.Now;

				// Connect to a queue on the local computer.
				MessageQueue myQueue = new MessageQueue(".\\myQueue");



				// Create the new order.
				Message myMessage = new Message(sentOrder);

				// Send the order to the queue.
				myQueue.Send(myMessage);
			}
			catch(ArgumentException e)
			{
				Console.WriteLine(e.Message);

			}

			return;
		}


		//**************************************************
		// Receives a message containing an order.
		//**************************************************

		public  void ReceiveMessage()
		{
			// Connect to the a queue on the local computer.
			MessageQueue myQueue = new MessageQueue(".\\myQueue");

			// Set the formatter to indicate body contains an Order.
			myQueue.Formatter = new XmlMessageFormatter(new Type[]
				{typeof(MyProject.Order)});

			try
			{
				// Receive and format the message. 
				Message myMessage =	myQueue.Receive(); 
				Order myOrder = (Order)myMessage.Body;

				// Display message information.
				Console.WriteLine("Order ID: " + 
					myOrder.orderId.ToString());
				Console.WriteLine("Sent: " + 
					myOrder.orderTime.ToString());
			}

			catch (MessageQueueException)
			{
				// Handle Message Queuing exceptions.
			}

				// Handle invalid serialization format.
			catch (InvalidOperationException e)
			{
				Console.WriteLine(e.Message);
			}

			// Catch other exceptions as necessary.

			return;
		}
	}
}

.NET Framework
Available since 1.1
Return to top
Show: