MessageQueue.Send Method (Object, MessageQueueTransaction)

 

Sends an object to the transactional queue referenced by this MessageQueue.

Namespace:   System.Messaging
Assembly:  System.Messaging (in System.Messaging.dll)

public void Send(
	object obj,
	MessageQueueTransaction transaction
)

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;
using System.Messaging;

namespace MyProject
{

    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        //**************************************************
        // Provides an entry point into the application.
        // 
        // This example sends and receives a message from
        // a transactional queue.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Send a message to a queue.
            myNewQueue.SendMessageTransactional();

            // Receive a message from a queue.
            myNewQueue.ReceiveMessageTransactional();

            return;
        }


        //**************************************************
        // Sends a message to a queue.
        //**************************************************

        public void SendMessageTransactional()
        {

            // Connect to a queue on the local computer.
            MessageQueue myQueue = new 
                MessageQueue(".\\myTransactionalQueue");

            // Send a message to the queue.
            if (myQueue.Transactional == true)
            {
                // Create a transaction.
                MessageQueueTransaction myTransaction = new 
                    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.
        //**************************************************

        public  void ReceiveMessageTransactional()
        {
            // Connect to a transactional queue on the local computer.
            MessageQueue myQueue = new 
                MessageQueue(".\\myTransactionalQueue");

            // Set the formatter.
            myQueue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(String)});

            // Create a transaction.
            MessageQueueTransaction myTransaction = new 
                MessageQueueTransaction();

            try
            {
                // Begin the transaction.
                myTransaction.Begin();

                // Receive the message. 
                Message myMessage =	myQueue.Receive(myTransaction); 
                String myOrder = (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;
        }
    }
}

.NET Framework
Available since 1.1
Return to top
Show: