How to create a custom message formatter by using Visual C# (310683)



The information in this article applies to:

  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# 2005, Express Edition

This article was previously published under Q310683
This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Messaging

IN THIS TASK

SUMMARY

This step-by-step article shows you how to use Visual C# to create a simple custom message formatter.

The System.Messaging namespace provides classes that allow you to connect to message queues, send messages, and receive or peek messages from queues. When you send a message to the queue, objects are serialized by the message formatter into the body of the message. The System.Messaging namespace includes the following three formatters:
  • XmlMessageFormatter(default)
  • BinaryMessageFormatter
  • ActiveXMessageFormatter
In certain situations, you may need to develop a custom message formatter that can serialize strings into the body of the message and deserialize strings when messages are read from the queue.

To create a custom message formatter, you must implement the IMessageFormatter interface, which is included in the System.Messaging namespace. This interface specifies the following three methods:
  • Write
  • Read
  • CanRead
Because this interface implements the ICloneable interface, the Clone method must also be implemented.

back to the top

Class declaration

In this example, the formatter class is called MyFormatter, and is declared as follows:
using System;
using System.IO;
using System.Text;
using System.Messaging;

public class MyFormatter : IMessageFormatter
{

	public MyFormatter()
	{
		//The constructor can remain empty.
	}
}
				
Note that a project reference to System.Messaging.DLL is required. To add this reference in Visual Studio .NET, right-click References and select Add Reference.

back to the top

Write method

The Write method serializes objects into the body of the message when the message is sent to the queue. In this example, the formatter takes in a string and serializes it by using UTF8 encoding. The Write method is implemented as follows:
public void Write (System.Messaging.Message msg, object obj)
{
	//Declare a buffer.
	byte[] buff;

	//Place the string into the buffer using UTF8 encoding.
	buff = Encoding.UTF8.GetBytes (obj.ToString());

	//Create a new MemoryStream object passing the buffer.
	Stream stm = new MemoryStream(buff);

	//Assign the stream to the message's BodyStream property.
	msg.BodyStream = stm;
} 
				
back to the top

Read method

The Read method deserializes the body of the message when you read or peek the message from the queue. Essentially, the Read method reverses the process that is done with the Write method.
public object Read (System.Messaging.Message msg)
{
	//Obtain the BodyStream for the message.
	Stream stm = msg.BodyStream;

	//Create a StreamReader object used for reading from the stream.
	StreamReader reader = new StreamReader (stm);

	//Return the string read from the stream.
	//StreamReader.ReadToEnd returns a string.
	return reader.ReadToEnd();
}
				
back to the top

CanRead method

The CanRead method attempts to determine whether the formatter can handle the message that is passed to it. For brevity, this formatter assumes that the message body is a string (all messages in the queue were sent using this formatter) and returns True.
public bool CanRead (System.Messaging.Message msg)
{
	return true;
}
				
back to the top

Clone method

The Clone method returns a new instance of the formatter.
public object Clone()
{
	return new MyFormatter();
}
				
back to the top

Use the formatter

After the formatter is implemented, you can set the Formatter property of the MessageQueue object to a new instance of this formatter, as follows:
//Open an existing public queue called Test.	
MessageQueue msgQueue = new MessageQueue (@".\test");
//Set the Formatter property.
msgQueue.Formatter = new MyFormatter();
//Send a test message.
msgQueue.Send ("Test string message");
				
back to the top

Modification Type:MinorLast Reviewed:1/7/2006
Keywords:kbBCL kbHOWTOmaster kbSample KB310683 kbAudDeveloper