Message Constructor ()
Initializes a new instance of the Message class with an empty body.
Assembly: System.Messaging (in System.Messaging.dll)
Use this overload to create a new instance of the Message class that has an empty body.
Specify either the Body property or the BodyStream property before sending the Message object. The Body property can be any object that can be serialized, such as a text string, a structure object, a class instance, or an embedded object.
Unless you write the contents of the message directly to the BodyStream property, set the Formatter property before you send the message. The body is serialized using the Formatter property's value at the time the Send method is called on the MessageQueue instance.
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.
The following table shows initial property values for an instance of Message.
Property | Initial value |
|---|---|
AcknowledgeType.None | |
null | |
0 | |
true | |
Microsoft Base Cryptographic Provider version 1.0 | |
CryptoProviderType.RSA_FULL | |
null | |
Stream.null | |
0 | |
Guid.Empty | |
An empty string ("") | |
A zero-length array of bytes | |
A zero-length array of bytes | |
EncryptionAlgorithm.RC2 | |
A zero-length array of bytes | |
XmlMessageFormatter | |
HashAlgorithm.MD5 | |
An empty string ("") | |
MessagePriority.Normal | |
false | |
null | |
A zero-length array of bytes | |
Message.InfiniteTimeout | |
Message.InfiniteTimeout | |
null | |
false | |
false | |
false | |
false | |
false |
The following code example sends two messages of different priorities to the queue, and retrieves them subsequently.
#using <system.dll> #using <system.messaging.dll> using namespace System; using namespace System::Messaging; /// <summary> /// Provides a container class for the example. /// </summary> ref class MyNewQueue { //************************************************** // Sends a string message to a queue. //************************************************** public: void SendMessage( MessagePriority priority, String^ messageBody ) { // Connect to a queue on the local computer. MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" ); // Create a new message. Message^ myMessage = gcnew Message; if ( priority > MessagePriority::Normal ) { myMessage->Body = "High Priority: {0}",messageBody; } else { myMessage->Body = messageBody; } // Set the priority of the message. myMessage->Priority = priority; // Send the Order to the queue. myQueue->Send( myMessage ); return; } //************************************************** // Receives a message. //************************************************** void ReceiveMessage() { // Connect to the a queue on the local computer. MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" ); // Set the queue to read the priority. By default, it // is not read. myQueue->MessageReadPropertyFilter->Priority = true; // Set the formatter to indicate body contains a String^. array<Type^>^ p = gcnew array<Type^>(1); p[ 0 ] = String::typeid; myQueue->Formatter = gcnew XmlMessageFormatter( p ); try { // Receive and format the message. Message^ myMessage = myQueue->Receive(); // Display message information. Console::WriteLine( "Priority: {0}", myMessage->Priority ); Console::WriteLine( "Body: {0}", myMessage->Body ); } catch ( MessageQueueException^ ) { // Handle Message Queuing exceptions. } // Handle invalid serialization format. catch ( InvalidOperationException^ e ) { Console::WriteLine( e->Message ); } // Catch other exceptions as necessary. return; } }; //************************************************** // Provides an entry point into the application. // // This example sends and receives a message from // a queue. //************************************************** int main() { // Create a new instance of the class. MyNewQueue^ myNewQueue = gcnew MyNewQueue; // Send messages to a queue. myNewQueue->SendMessage( MessagePriority::Normal, "First Message Body." ); myNewQueue->SendMessage( MessagePriority::Highest, "Second Message Body." ); // Receive messages from a queue. myNewQueue->ReceiveMessage(); myNewQueue->ReceiveMessage(); return 0; }
Available since 1.1