Message Class
Represents the unit of communication between endpoints in a distributed environment.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
The Message type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() | Headers | When overridden in a derived class, gets the headers of the message. |
![]() ![]() | IsDisposed | Returns a value that indicates whether the Message is disposed. |
![]() ![]() | IsEmpty | Returns a value that indicates whether the Message is empty. |
![]() ![]() | IsFault | Gets a value that indicates whether this message generates any SOAP faults. |
![]() ![]() | Properties | When overridden in a derived class, gets a set of processing-level annotations to the message. |
![]() ![]() | State | Gets the current state of this Message. |
![]() ![]() | Version | When overridden in a derived class, gets the SOAP version of the message. |
| Name | Description | |
|---|---|---|
![]() ![]() | Close | Closes the Message and releases any resources. |
![]() | CreateBufferedCopy | Stores an entire Message into a memory buffer for future access. |
![]() ![]() ![]() | CreateMessage(MessageVersion, String) | Creates a message that contains a version and an action. |
![]() ![]() | CreateMessage(MessageVersion, MessageFault, String) | Creates a message that contains a SOAP fault, a version and an action. |
![]() ![]() ![]() | CreateMessage(MessageVersion, String, Object) | Creates a message with the specified version, action and body. |
![]() ![]() ![]() | CreateMessage(MessageVersion, String, BodyWriter) | Creates a message with a body that consists of an array of bytes. |
![]() ![]() ![]() | CreateMessage(MessageVersion, String, XmlDictionaryReader) | Creates a message with the specified version, action and body. |
![]() ![]() ![]() | CreateMessage(MessageVersion, String, XmlReader) | Creates a message using the specified reader, action and version. |
![]() ![]() | CreateMessage(XmlDictionaryReader, Int32, MessageVersion) | Creates a message using the specified reader, action and version. |
![]() ![]() | CreateMessage(XmlReader, Int32, MessageVersion) | Creates a message using the specified reader, action and version. |
![]() ![]() | CreateMessage(MessageVersion, FaultCode, String, String) | Creates a message that contains a SOAP fault, the reason for the fault, a version and an action. |
![]() ![]() ![]() | CreateMessage(MessageVersion, String, Object, XmlObjectSerializer) | Creates a message using the specified version, action, message body and serializer. |
![]() ![]() | CreateMessage(MessageVersion, FaultCode, String, Object, String) | Creates a message that contains a SOAP fault, a reason and the detail for the fault, version and action. |
![]() ![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() ![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() ![]() | GetBody<T>() | Retrieves the body of this Message instance. |
![]() ![]() | GetBody<T>(XmlObjectSerializer) | Retrieves the body of this Message using the specified serializer. |
![]() ![]() | GetBodyAttribute | Retrieves the attributes of the message body. |
![]() ![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() ![]() | GetReaderAtBodyContents | Gets the XML dictionary reader that accesses the body content of this message. |
![]() ![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() ![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() ![]() | OnBodyToString | Called when the message body is converted to a string. |
![]() ![]() | OnClose | Called when the message is closing. |
![]() | OnCreateBufferedCopy | Called when a message buffer is created to store this message. |
![]() ![]() | OnGetBodyAttribute | Called when the attributes of the message body is retrieved. |
![]() ![]() | OnGetReaderAtBodyContents | Called when an XML dictionary reader that can access the body content of this message can be retrieved. |
![]() ![]() | OnWriteBodyContents | Called when the message body is written to an XML file. |
![]() ![]() | OnWriteMessage | Called when the entire message is written to an XML file. |
![]() ![]() | OnWriteStartBody | Called when the start body is written to an XML file. |
![]() ![]() | OnWriteStartEnvelope | Called when the start envelope is written to an XML file. |
![]() ![]() | OnWriteStartHeaders | Called when the start header is written to an XML file. |
![]() ![]() | ToString | Returns a string that represents the current Message. (Overrides Object::ToString().) |
![]() ![]() | WriteBody(XmlDictionaryWriter) | Writes the body element using the specified XmlDictionaryWriter. |
![]() ![]() | WriteBody(XmlWriter) | Serializes the message body using the specified XmlWriter. |
![]() ![]() | WriteBodyContents | Serializes the body content using the specified XmlDictionaryWriter. |
![]() ![]() | WriteMessage(XmlDictionaryWriter) | Serializes the entire message using the specified XmlDictionaryWriter. |
![]() ![]() | WriteMessage(XmlWriter) | Serializes the entire message using the specified XmlWriter. |
![]() ![]() | WriteStartBody(XmlDictionaryWriter) | Serializes the start body using the specified XmlDictionaryWriter. |
![]() ![]() | WriteStartBody(XmlWriter) | Serializes the start body using the specified XmlDictionaryWriter. |
![]() ![]() | WriteStartEnvelope | Serializes the start envelope using the specified XmlDictionaryWriter. |
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | IDisposable::Dispose | Closes all the resources used by this message. This method cannot be inherited. |
The Message class provides a means of communicating arbitrary information between a sender and a receiver on a network. It can be used to relay information, suggest or demand a course of action, or request data.
The structure of a Message object represents a SOAP envelope. It consists of two distinct parts: the message's body and an optional collection of headers, represented by the Headers class. The message content is application-defined data sent from a sender to a receiver. The message headers enable system and application extensibility to meet the changing requirements, because you can define code to manipulate and respond to specific headers. You can also define your own headers. Message headers are serialized or deserialized along with the contents of the message.
Messages are received and sent in particular formats. Support is provided for two formats: the standard text-based XML format and a binary-based XML format. The Message object can be used to represent both SOAP 1.1 and SOAP 1.2 envelopes. Note that an instance of Message is fixed upon creation and is bound to a specific SOAP version. The Version property represents the SOAP version of the message.
A Message object can be serialized to an external store by using the WriteMessage method. Properties of the message can also be serialized, but they have to be individually identified and serialized separately. Deserializing a message to create an in-memory Message object can be done using CreateMessage. Properties must also be deserialized individually and manually added to the property collection for the specific Message instance.
The size of a Message object is fixed to the size of data it is transmitting. Every body is modeled as an instance of XmlReader, with no predefined limit on the size of the stream that the XmlReader instance is wrapping. However, specific channel providers can have a limit on the size of messages that they process.
A Message can be annotated with useful information generated by an entity that has examined and processed the message. This functionality is provided by the Headers and Properties properties. The Headers collection represents the set of SOAP headers on the message.
The Properties property represents the set of processing-level annotations on the message. Because information in headers is transmitted on the wire, an entity that examines a header must support the underlying version(s) of the protocols used by the header. However, properties provide a more version-independent way of annotating a message.
To create a Message instance, use one of the CreateMessage methods.
It is recommended that a consumer of a message always call Close when the consumer is finished accessing the contents of the message. This action frees finite system resources (for example, sockets, named pipes) that are tied to the lifetime of the message.
Special note for Managed C++ users deriving from this class:
Put your cleanup code in (On)(Begin)Close (and/or OnAbort), not in a destructor.
Avoid destructors: they cause the compiler to auto-generate IDisposable.
Avoid non-reference members: they can cause the compiler to auto-generate IDisposable.
Avoid finalizers; but if you include one, suppress the build warning and call SuppressFinalize(Object) and the finalizer itself from (On)(Begin)Close (and/or OnAbort) to emulate what would have been the auto-generated IDisposable behavior.
When you inherit from Message, you must override the following members: Headers, and Version.
The following code example shows a client that uses the channel factory to send a message and read the reply.
#using <System.Messaging.dll> #using <System.dll> using namespace System; using namespace System::Messaging; public ref class Order { private: int orderIdValue; private: DateTime orderTimeValue; public: property int OrderId { int get() { return orderIdValue; } void set(int value) { orderIdValue = value; } } public: property DateTime OrderTime { DateTime get() { return orderTimeValue; } void set(DateTime value) { orderTimeValue = value; } } public: Order() { } }; // Creates a new queue. void CreateQueue(String^ queuePath, bool transactional) { if(!MessageQueue::Exists(queuePath)) { MessageQueue^ queue = MessageQueue::Create(queuePath, transactional); queue->Close(); } else { Console::WriteLine("{0} already exists.", queuePath); } } // Sends an Order to a queue. void SendMessage() { // 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^ queue = gcnew MessageQueue(".\\orderQueue"); // Create the new order. Message^ orderMessage = gcnew Message(sentOrder); // Set the message's TimeToReachQueue property to five minutes. orderMessage->TimeToReachQueue = TimeSpan::FromMinutes(5.0); // Display the new value of the message's TimeToReachQueue property. Console::WriteLine("Message.TimeToReachQueue: {0}", orderMessage->TimeToReachQueue.ToString()); // Set the message's TimeToBeReceived property to five minutes. orderMessage->TimeToBeReceived = TimeSpan::FromMinutes(5.0); // Display the new value of the message's TimeToBeReceived property. Console::WriteLine("Message.TimeToBeReceived: {0}", orderMessage->TimeToBeReceived.ToString()); // Set the message's UseTracing property to true. orderMessage->UseTracing = true; // Display the new value of the message's UseTracing property. Console::WriteLine("Message.UseTracing: {0}", orderMessage->UseTracing); // Set the message's Label property. orderMessage->Label = "Order Message"; // Display the new value of the message's Label property. Console::WriteLine("Message Label: {0}", orderMessage->Label); // Set the message's AttachSenderId property. orderMessage->AttachSenderId = true; // Display the new value of the message's AttachSenderId property. Console::WriteLine("Message.AttachSenderId: {0}", orderMessage->AttachSenderId); // Set the message's Recoverable property. orderMessage->Recoverable = true; // Display the new value of the message's Recoverable property. Console::WriteLine("Message.Recoverable: {0}", orderMessage->Recoverable); // Set the message's ResponseQueue property. // (You must have a connection to a queue.) orderMessage->ResponseQueue = queue; // Display the new value of the message's ResponseQueue property. Console::WriteLine("Message.ResponseQueue.QueueName: {0}", orderMessage->ResponseQueue->QueueName); // Set the message's TimeToReachQueue property to // Message.InfiniteTimeout. orderMessage->TimeToReachQueue = Message::InfiniteTimeout; // Display the new value of the message's TimeToReachQueue property. Console::WriteLine("Message.TimeToReachQueue: {0}", orderMessage->TimeToReachQueue.ToString()); // Set the message's UseDeadLetterQueue property. orderMessage->UseDeadLetterQueue = true; // Display the new value of the message's UseDeadLetterQueue property. Console::WriteLine("Message.UseDeadLetterQueue: {0}", orderMessage->UseDeadLetterQueue); // Set the message's UseJournalQueue property. orderMessage->UseJournalQueue = true; // Display the new value of the message's UseJournalQueue property. Console::WriteLine("Message.UseJournalQueue: {0}", orderMessage->UseJournalQueue); // Set the message's UseEncryption property. orderMessage->UseEncryption = true; // Display the new value of the message's UseEncryption property. Console::WriteLine("Message.UseEncryption: {0}", orderMessage->UseEncryption); // Send the order to the queue. queue->Send(orderMessage); queue->Close(); } // Receives a message containing an order. void ReceiveMessage() { // Connect to a queue on the local computer. MessageQueue^ queue = gcnew MessageQueue(".\\orderQueue"); // Set the formatter to indicate the message body contains an order. queue->Formatter = gcnew XmlMessageFormatter(gcnew array<Type^> {Order::typeid}); // Set the queue's MessageReadPropertyFilter properties to ensure that // the message includes the properties in which we are interested. queue->MessageReadPropertyFilter->ArrivedTime = true; queue->MessageReadPropertyFilter->DestinationQueue = true; queue->MessageReadPropertyFilter->SenderVersion = true; queue->MessageReadPropertyFilter->SentTime = true; queue->MessageReadPropertyFilter->SourceMachine = true; queue->MessageReadPropertyFilter->Authenticated = true; // Receive the message. Time out after ten seconds in case the message // was not delivered. Message^ orderMessage = queue->Receive(TimeSpan::FromSeconds(10.0)); // Format the message. Order^ sentOrder = (Order^)orderMessage->Body; // Display message information. Console::WriteLine("Order ID: {0}", sentOrder->OrderId.ToString()); Console::WriteLine("Sent: {0}", sentOrder->OrderTime.ToString()); // Display the value of the message's MessageType property. Console::WriteLine("Message Type: {0}", orderMessage->MessageType); // Display the value of the message's SourceMachine property. // To view this property value, the queue's // MessageReadPropertyFilter.SourceMachine property must be set to // true before the message is received. Console::WriteLine("Source Machine: {0}", orderMessage->SourceMachine); // Display the value of the message's DestinationQueue property. // To view this property value, the queue's // MessageReadPropertyFilter.DestinationQueue property must be set to // true before the message is received. Console::WriteLine("Message.DestinationQueue.QueueName: {0}", orderMessage->DestinationQueue->QueueName); // Display the value of the message's SentTime property. // To view this property value, the queue's // MessageReadPropertyFilter.SentTime property must be set to // true before the message is received. Console::WriteLine("Time sent from source queue: {0}", orderMessage->SentTime.ToString()); // Display the value of the message's ArrivedTime property. // To view this property value, the queue's // MessageReadPropertyFilter.ArrivedTime property must be set to // true before the message is received. Console::WriteLine("Time arrived in destination queue: {0}", orderMessage->ArrivedTime.ToString()); // Display the value of the message's Authenticated property. // To view this property value, the queue's // MessageReadPropertyFilter.Authenticated property must be set to // true before the message is received. Console::WriteLine("Authentication requested for this message: {0}", orderMessage->Authenticated.ToString()); // Display the value of the message's SenderVersion property. // To view this property value, the queue's // MessageReadPropertyFilter.SenderVersion property must be set to // true before the message is received. Console::WriteLine("Message.SenderVersion: {0}", orderMessage->SenderVersion); // Message Queuing sets this property when the message is sent. // Display the value of the message's BodyType property. Console::WriteLine("Message.BodyType: {0}", orderMessage->BodyType.ToString()); queue->Close(); } // Sends an Order to a transactional queue. void SendMessageToTransQueue() { // 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^ queue = gcnew MessageQueue(".\\orderTransQueue"); // Create the new order. Message^ orderMessage = gcnew Message(sentOrder); // Create a message queuing transaction. MessageQueueTransaction^ transaction = gcnew MessageQueueTransaction(); try { // Begin a transaction. transaction->Begin(); // Send the order to the queue. queue->Send(orderMessage, transaction); // Commit the transaction. transaction->Commit(); } catch (MessageQueueException^ ex) { // Abort the transaction. transaction->Abort(); // Propagate the exception. throw; } finally { // Dispose of the transaction object. delete transaction; queue->Close(); } } // Receives a message containing an order. void ReceiveMessageFromTransQueue() { Message^ orderMessage; // Connect to the a queue on the local computer. MessageQueue^ queue = gcnew MessageQueue(".\\orderTransQueue"); // Set the formatter to indicate the message body contains an order. queue->Formatter = gcnew XmlMessageFormatter(gcnew array<Type^> {Order::typeid}); // Set the queue's MessageReadPropertyFilter properties to ensure that // the message includes the properties in which we are interested. queue->MessageReadPropertyFilter->TransactionId = true; queue->MessageReadPropertyFilter->TransactionStatusQueue = true; queue->MessageReadPropertyFilter->IsFirstInTransaction = true; queue->MessageReadPropertyFilter->IsLastInTransaction = true; // Create a message queuing transaction. MessageQueueTransaction^ transaction = gcnew MessageQueueTransaction(); try { // Begin a transaction. transaction->Begin(); // Receive the message. Time out after ten seconds in case the // message was not delivered. orderMessage = queue->Receive(TimeSpan::FromSeconds(10.0), transaction); // Commit the transaction. transaction->Commit(); } catch (MessageQueueException^ ex) { // Abort the transaction. transaction->Abort(); // Propagate the exception. throw; } finally { // Dispose of the transaction object. delete transaction; queue->Close(); } // Message Queuing sets this property when the message is sent. // Display the value of the message's TransactionStatusQueue property. // To view this property value, the queue's // MessageReadPropertyFilter.TransactionStatusQueue property must be // set to true before the message is received. Console::WriteLine("Message.TransactionStatusQueue.QueueName: {0}", orderMessage->TransactionStatusQueue->QueueName); // Display the value of the message's TransactionId property. // To view this property value, the queue's // MessageReadPropertyFilter.TransactionId property must be set to true // before the message is received. Console::WriteLine("Message.TransactionId: {0}", orderMessage->TransactionId); // Display the value of the message's IsFirstInTransaction property. // To view this property value, the queue's // MessageReadPropertyFilter.IsFirstInTransaction property must be set // to true before the message is received. Console::WriteLine("Message.IsFirstInTransaction: {0}", orderMessage->IsFirstInTransaction); // Display the value of the message's IsLastInTransaction property. // To view this property value, the queue's // MessageReadPropertyFilter.IsLastInTransaction property must be set // to true before the message is received. Console::WriteLine("Message.IsLastInTransaction: {0}", orderMessage->IsLastInTransaction); } int main() { try { // Create a non-transactional queue on the local computer. // Note that the queue might not be immediately accessible, and // therefore this example might throw an exception of type // System.Messaging.MessageQueueException when trying to send a // message to the newly created queue. CreateQueue(".\\orderQueue", false); // Send a message to a queue. SendMessage(); // Receive a message from a queue. ReceiveMessage(); // Create a transactional queue on the local computer. CreateQueue(".\\orderTransQueue", true); // Send a message to a transactional queue. SendMessageToTransQueue(); // Receive a message from a transactional queue. ReceiveMessageFromTransQueue(); } catch (InvalidOperationException^) { Console::WriteLine("Please install Message Queuing."); } catch (MessageQueueException^ ex) { // Write the exception information to the console. Console::WriteLine(ex); } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

