Export (0) Print
Expand All
1 out of 72 rated this helpful - Rate this topic

MessageQueue.Send Method

Sends an object to a queue.

Overload List

Sends an object to nontransactional queue referenced by this MessageQueue.

[Visual Basic] Overloads Public Sub Send(Object)
[C#] public void Send(object);
[C++] public: void Send(Object*);
[JScript] public function Send(Object);

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

[Visual Basic] Overloads Public Sub Send(Object, MessageQueueTransaction)
[C#] public void Send(object, MessageQueueTransaction);
[C++] public: void Send(Object*, MessageQueueTransaction*);
[JScript] public function Send(Object, MessageQueueTransaction);

Sends an object to the queue referenced by this MessageQueue.

[Visual Basic] Overloads Public Sub Send(Object, MessageQueueTransactionType)
[C#] public void Send(object, MessageQueueTransactionType);
[C++] public: void Send(Object*, MessageQueueTransactionType);
[JScript] public function Send(Object, MessageQueueTransactionType);

Sends an object to the nontransactional queue referenced by this MessageQueue and specifies a label for the message.

[Visual Basic] Overloads Public Sub Send(Object, String)
[C#] public void Send(object, string);
[C++] public: void Send(Object*, String*);
[JScript] public function Send(Object, String);

Sends an object to the transactional queue referenced by this MessageQueue and specifies a label for the message.

[Visual Basic] Overloads Public Sub Send(Object, String, MessageQueueTransaction)
[C#] public void Send(object, string, MessageQueueTransaction);
[C++] public: void Send(Object*, String*, MessageQueueTransaction*);
[JScript] public function Send(Object, String, MessageQueueTransaction);

Sends an object to the queue referenced by this MessageQueue and specifies a label for the message.

[Visual Basic] Overloads Public Sub Send(Object, String, MessageQueueTransactionType)
[C#] public void Send(object, string, MessageQueueTransactionType);
[C++] public: void Send(Object*, String*, MessageQueueTransactionType);
[JScript] public function Send(Object, String, MessageQueueTransactionType);

Example

[Visual Basic, C#, C++] The following example sends a string to a transactional queue and then receives a message from that queue.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Send. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports 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 Shared Sub Main()

            ' Create a new instance of the class.
            Dim myNewQueue As New MyNewQueue()

            ' Send a message to a queue.
            myNewQueue.SendMessageTransactional()

            ' Receive a message from a queue.
            myNewQueue.ReceiveMessageTransactional()

            Return

        End Sub 'Main


        '**************************************************
        ' Sends a message to a queue.
        '**************************************************

        Public Sub SendMessageTransactional()

            ' Connect to a queue on the local computer.
            Dim myQueue As New MessageQueue(".\myTransactionalQueue")

            ' Send a message to the queue.
            If myQueue.Transactional = True Then
                myQueue.Send("My Message Data.", New _
                    MessageQueueTransaction())
            End If

            Return

        End Sub 'SendMessageTransactional


        '**************************************************
        ' Receives a message containing an Order.
        '**************************************************

        Public Sub ReceiveMessageTransactional()

            ' Connect to a transactional queue on the local computer.
            Dim myQueue As New MessageQueue(".\myTransactionalQueue")

            ' Set the formatter.
            myQueue.Formatter = New XmlMessageFormatter(New Type() _
                {GetType([String])})

            ' Create a transaction.
            Dim myTransaction As New MessageQueueTransaction()

            Try

                ' Begin the transaction.
                myTransaction.Begin()

                ' Receive the message. 
                Dim myMessage As Message = _
                    myQueue.Receive(myTransaction)
                Dim myOrder As [String] = CType(myMessage.Body, _
                    [String])

                ' Display message information.
                Console.WriteLine(myOrder)

                ' Commit the transaction.
                myTransaction.Commit()


            Catch e As MessageQueueException

                ' Handle nontransactional queues.
                If e.MessageQueueErrorCode = _
                    MessageQueueErrorCode.TransactionUsage Then

                    Console.WriteLine("Queue is not transactional.")

                End If

                ' Else catch other sources of a MessageQueueException.


                ' Roll back the transaction.
                myTransaction.Abort()


                ' Catch other exceptions as necessary, such as 
                ' InvalidOperationException, thrown when the formatter
                ' cannot deserialize the message.

            End Try

            Return

        End Sub 'ReceiveMessageTransactional

    End Class 'MyNewQueue
End Namespace 'MyProject

[C#] 
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)
            {
                myQueue.Send("My Message Data.", new 
                    MessageQueueTransaction());
            }

            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;
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;

/// <summary>
/// Provides a container class for the example.
/// </summary>
__gc class MyNewQueue 
{
    //*************************************************
    // Sends a message to a queue.
    //*************************************************
public:
    void SendMessageTransactional() 
    {
        // Connect to a queue on the local computer.
        MessageQueue* myQueue = new MessageQueue(S".\\myTransactionalQueue");

        // Send a message to the queue.
        if (myQueue->Transactional == true) 
        {
            myQueue->Send(S"My Message Data.", new MessageQueueTransaction());
        }

        return;
    }

    //*************************************************
    // Receives a message containing an Order.
    //*************************************************
public:
    void ReceiveMessageTransactional() 
    {
        // Connect to a transactional queue on the local computer.
        MessageQueue* myQueue = new MessageQueue(S".\\myTransactionalQueue");

        // Set the formatter.
        Type* p __gc[] = new Type* __gc[1];
        p[0] = __typeof(String);
        myQueue->Formatter = new XmlMessageFormatter( p );

        // Create a transaction.
        MessageQueueTransaction* myTransaction = new 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(S"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 = new MyNewQueue();

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

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

    return 0;
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

MessageQueue Class | MessageQueue Members | System.Messaging Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.