MessageQueue::Send Method (Object^, MessageQueueTransaction^)
Sends an object to the transactional queue referenced by this MessageQueue.
Assembly: System.Messaging (in System.Messaging.dll)
Parameters
- obj
-
Type:
System::Object^
The object to send to the queue.
- transaction
-
Type:
System.Messaging::MessageQueueTransaction^
The MessageQueueTransaction object.
| Exception | Condition |
|---|---|
| ArgumentNullException | The transaction parameter is null. |
| MessageQueueException | The Path property has not been set. -or- The Message Queuing application indicated an incorrect transaction use. -or- An error occurred when accessing a Message Queuing method. |
Use this overload to send a message that contains the obj parameter to the transactional queue referenced by the MessageQueue, using an internal transaction context defined by the transaction parameter. The object you send to the queue can be a Message or any managed object. If you send any object other than a Message, the object is serialized and inserted into the body of the message.
If you use this overload to send a message to a non-transactional queue, the message might be sent to the dead-letter queue without throwing an exception.
If you do not set the Formatter property before calling Send(Object^), the formatter defaults to the XmlMessageFormatter.
The DefaultPropertiesToSend property applies to any object other than a Message. If you specify, for example, a label or a priority using the DefaultPropertiesToSend member, these values apply to any message that contains an object that is not of type Message when your application sends it to the queue. When sending a Message, the property values set for the Message take precedence over DefaultPropertiesToSend and the message's Message::Formatter property takes precedence over the queue's MessageQueue::Formatter property.
[Visual Basic]
MessageQueueTransaction is threading apartment aware, so if your apartment state is STA, you cannot use the transaction in multiple threads. Visual Basic sets the state of the main thread to STA, so you must apply the MTAThreadAttribute in the Main subroutine. Otherwise, sending a transactional message using another thread throws a MessageQueueException exception. You apply the MTAThreadAttribute by using the following fragment.
<System.MTAThreadAttribute> public sub Main()
The following table shows whether this method is available in various Workgroup modes.
Workgroup mode | Available |
|---|---|
Local computer | Yes |
Local computer and direct format name | Yes |
Remote computer | No |
Remote computer and direct format name | Yes |
The following code example sends a string to a transactional queue and then receives a message from that queue.
#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 { public: //************************************************* // Sends a message to a queue. //************************************************* void SendMessageTransactional() { // Connect to a queue on the local computer. MessageQueue^ myQueue = gcnew MessageQueue( ".\\myTransactionalQueue" ); // Send a message to the queue. if ( myQueue->Transactional == true ) { // Create a transaction. MessageQueueTransaction^ myTransaction = gcnew MessageQueueTransaction; // Begin the transaction. myTransaction->Begin(); // Send the message. myQueue->Send( "My Message Data.", myTransaction ); // Commit the transaction. myTransaction->Commit(); } return; } //************************************************* // Receives a message containing an Order. //************************************************* void ReceiveMessageTransactional() { // Connect to a transactional queue on the local computer. MessageQueue^ myQueue = gcnew MessageQueue( ".\\myTransactionalQueue" ); // Set the formatter. array<Type^>^p = gcnew array<Type^>(1); p[ 0 ] = String::typeid; myQueue->Formatter = gcnew XmlMessageFormatter( p ); // Create a transaction. MessageQueueTransaction^ myTransaction = gcnew MessageQueueTransaction; try { // Begin the transaction. myTransaction->Begin(); // Receive the message. Message^ myMessage = myQueue->Receive( myTransaction ); String^ myOrder = static_cast<String^>(myMessage->Body); // Display message information. Console::WriteLine( myOrder ); // Commit the transaction. myTransaction->Commit(); } catch ( MessageQueueException^ e ) { // Handle nontransactional queues. if ( e->MessageQueueErrorCode == MessageQueueErrorCode::TransactionUsage ) { Console::WriteLine( "Queue is not transactional." ); } // Else catch other sources of MessageQueueException. // Roll back the transaction. myTransaction->Abort(); } // Catch other exceptions as necessary, such as // InvalidOperationException, thrown when the formatter // cannot deserialize the message. return; } }; //************************************************* // Provides an entry point into the application. // // This example sends and receives a message from // a transactional queue. //************************************************* int main() { // Create a new instance of the class. MyNewQueue^ myNewQueue = gcnew MyNewQueue; // Send a message to a queue. myNewQueue->SendMessageTransactional(); // Receive a message from a queue. myNewQueue->ReceiveMessageTransactional(); return 0; }
Available since 1.1