The release number of this article has been CHS310683
This task content
summary
Class declaration WRITE method read method canread method Clone method use formatting program
SUMMARY This step-by-step guide describes how to create a simple custom message formatting program using Visual C # .NET.
System.Messaging Namespaces Provides some classes, you can use these classes to connect to message queues, send messages, and receive or receive messages from queues. When you send a message to the queue, the message formatting program will sequence the object as a message body.
System.Messaging namespace includes the following three formatting procedures:
XmlMessageFormatter (default) BinaryMessageFormatter ActiveXMessageFormatter In some cases, you may need to create a custom message formatting program that can sequence string into messages and can read messages from the queue, Reverse sequencing.
To create a custom message formatting program, you must implement
IMESSAGEFORMATTER interface, the interface is included
System.MESSAGING namespace. This interface specifies the following three methods:
Write read canread because this interface is implemented
Icloneable interface, so you must also be implemented
Clone method.
Back to top
Class declaration In this example, the formatting program class is called
MyFormatter is declared as:
Using system;
Using system.io;
Using system.text;
Using system.Messaging;
Public Class MyFormatter: iMessageFormatter
{
Public myformatter ()
{
// The constructor can remain empty.
}
} Please note that
The project reference for system.Messaging.dll is essential. To add this reference to Visual Studio .NET, use the right click
Quote, then select
Add a reference.
Back to top
When the WRITE method sends a message to the queue,
The WRITE method can sequence the object to the body of the message. In this example, the formatting program receives a string and serializes it with UTF8 encoding.
The implementation of the WRITE method is 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.
Street STM = New MemoryStream (BUFF);
// Assign The stream to the message's body .tream.
Msg.Bodystream = STM;
}
Back to top
The READ method reads or views the message from the queue,
The read method will define the message body. basically,
Read method reversely executed
The process of using 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 forread from the stretam.
StreamReader Reader = New StreamReader (STM);
// Return The String Read from The Stream.
//StreamReader.ReadToend Returns a string.
Return Reader.ReadToend ();
}
Back to top
CANREAD method
The CanRead method is used to try to determine if the formatting program can handle the message passed to it. For the sake of simplicity, this format program assumes that the text is a string (all messages in the queue are sent by this formatting program) and returns True.
Public bool canread (System.Messaging.Message MSG)
{
Return True;
}
Back to top
Clone method
Clone method returns a new instance of the formatter.
Public Object Clone ()
{
Return new myformatter ();
}
Back to top
After using the formatting program to implement the formatting program, you can
MessageQueue object
The Formatter property is set to this formatting new instance, as shown below:
// Open an existing public queue caled 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 top
The information in this article applies to:
Microsoft Visual C # .NET (2002)
Reserved: 2002-2-24 (1.0) Keyword KBBCL KBDSupport Kbhowto KbhowTomaster KB310683