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

???? ID: 310683 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

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

TheSystem.Messagingnamespace 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. TheSystem.Messagingnamespace 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 theIMessageFormatterinterface, which is included in theSystem.Messaging??? ????? ??? This interface specifies the following three methods:
  • ????
  • ????
  • CanRead
Because this interface implements theICloneableinterface, theClonemethod must also be implemented.

Class declaration

In this example, the formatter class is calledMyFormatter, 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 toSystem.Messaging.DLLis required. To add this reference in Visual Studio .NET, right-click???????? ?? ??? ?????????? ??????.

Write method

The????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????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;
} 
				

Read method

The????method deserializes the body of the message when you read or peek the message from the queue. Essentially, the????method reverses the process that is done with the?????????? ???
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();
}
				

CanRead method

TheCanReadmethod 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;
}
				

Clone method

TheClonemethod returns a new instance of the formatter.
public object Clone()
{
	return new MyFormatter();
}
				

Use the formatter

After the formatter is implemented, you can set theFormatter?? ???MessageQueue???????? ?? formatter ?? ?? ?? ??????? ?? ?? ??? ???:
//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");
				

???

???? ID: 310683 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
??????: 
kbbcl kbhowtomaster kbsample kbmt KB310683 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:310683

??????????? ???