Message Constructor (Object^)
Initializes a new instance of the Message class, using the XmlMessageFormatter to serialize the specified object into the body of the message.
Assembly: System.Messaging (in System.Messaging.dll)
Parameters
- body
-
Type:
System::Object^
The object to be serialized into the body of the message.
Use this overload to create a new instance of the Message class that contains the Body specified by the body parameter. The body parameter can be any object that can be serialized, such as a text string, a structure object, a class instance, or an embedded object. The body is serialized using the XmlMessageFormatter unless you change the Formatter property before the Message is sent. If you change the Body or Formatter property at any time before calling Send, the message will be serialized according to the new property value.
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 | |
The body parameter. | |
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 creates a new queue, sends a message that contains an order to it, and then retrieves it.
#using <system.dll> #using <system.messaging.dll> #using <system.drawing.dll> using namespace System; using namespace System::Messaging; using namespace System::Drawing; using namespace System::IO; ref class Order { public: int orderId; DateTime orderTime; }; ref class MyNewQueue { public: static void CreateQueue( String^ queuePath ) { try { if ( !MessageQueue::Exists( queuePath ) ) { MessageQueue::Create( queuePath ); } else { Console::WriteLine( "{0} already exists.", queuePath ); } } catch ( MessageQueueException^ e ) { Console::WriteLine( e->Message ); } } void SendMessage() { try { // Create a new order and set values. Order^ sentOrder = gcnew Order; sentOrder->orderId = 3; sentOrder->orderTime = DateTime::Now; // Connect to a queue on the local computer. MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" ); // Create the new order. Message^ myMessage = gcnew Message( sentOrder ); // Send the order to the queue. myQueue->Send( myMessage ); } catch ( ArgumentException^ e ) { Console::WriteLine( e->Message ); } return; } void ReceiveMessage() { // Connect to the a queue on the local computer. MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" ); // Set the formatter to indicate body contains an Order. array<Type^>^p = gcnew array<Type^>(1); p[ 0 ] = Order::typeid; myQueue->Formatter = gcnew XmlMessageFormatter( p ); try { // Receive and format the message. Message^ myMessage = myQueue->Receive(); Order^ myOrder = dynamic_cast<Order^>(myMessage->Body); // Display message information. Console::WriteLine( "Order ID: {0}", myOrder->orderId ); Console::WriteLine( "Sent: {0}", myOrder->orderTime ); } catch ( MessageQueueException^ ) { // Handle Message Queuing exceptions. } // Handle invalid serialization format. catch ( InvalidOperationException^ e ) { Console::WriteLine( e->Message ); } // Catch other exceptions as necessary. return; } }; int main() { // Create a new instance of the class. MyNewQueue^ myNewQueue = gcnew MyNewQueue; // Create a queue on the local computer. MyNewQueue::CreateQueue( ".\\myQueue" ); // Send a message to a queue. myNewQueue->SendMessage(); // Receive a message from a queue. myNewQueue->ReceiveMessage(); return 0; }
Available since 1.1