.NET Framework Class Library
MessageQueue.Send Method (Object)

Sends an object to non-transactional queue referenced by this MessageQueue.

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

Syntax

Visual Basic (Declaration)
Public Sub Send ( _
    obj As Object _
)
Visual Basic (Usage)
Dim instance As MessageQueue
Dim obj As Object

instance.Send(obj)
C#
public void Send (
    Object obj
)
C++
public:
void Send (
    Object^ obj
)
J#
public void Send (
    Object obj
)
JScript
public function Send (
    obj : Object
)

Parameters

obj

The object to send to the queue.

Exceptions

Exception typeCondition

MessageQueueException

The Path property has not been set.

-or-

An error occurred when accessing a Message Queuing method.

Remarks

Use this overload to send a message that contains the obj parameter to the queue referenced by the MessageQueue. 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 transactional queue, the message will be sent to the dead-letter queue. If you want the message to be part of a transaction that contains other messages, use an overload that takes a MessageQueueTransaction or MessageQueueTransactionType as a parameter.

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.

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

NoteNote

This method is thread-safe when its object argument is a Message instance. The method is not thread-safe when its object argument is a non-Message instance.

Example

The following code example connects to a message queue and sends a message to the queue.

Visual Basic
Imports System
Imports System.Messaging

Public Class MyNewQueue


        '
        ' Provides an entry point into the application.
        ' 
        ' This example sends a message to a queue.
        '

        Public Shared Sub Main()

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

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

            Return

        End Sub 'Main


        '
        ' Sends a message to a queue.
        '

        Public Sub SendMessage()

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

            ' Send a message to the queue.
            If myQueue.Transactional = True Then

                ' Create a transaction.
                Dim myTransaction As New MessageQueueTransaction

                ' Begin the transaction.
                myTransaction.Begin()

                ' Send the message.
                myQueue.Send("My Message Data.", myTransaction)

                ' Commit the transaction.
                myTransaction.Commit()

            Else
                myQueue.Send("My Message Data.")
            End If

            Return

        End Sub 'SendMessage

End Class 'MyNewQueue
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 a message to a queue.
        /***************************************************/

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

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

            return;
        }


        /***************************************************/
        // Sends a message to a queue.
        /***************************************************/
        
        public void SendMessage()
        {
                        
            // Connect to a queue on the local computer.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");

            // 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();
            }
            else
            {
                myQueue.Send("My Message Data.");
            }

            return;
        }
    }
}
C++
#using <system.dll>
#using <system.messaging.dll.>

using namespace System;
using namespace System::Messaging;
ref class MyNewQueue
{
public:
   void SendMessage()
   {
      
      // Connect to a queue on the local computer.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );
      
      // 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();
      }
      else
      {
         myQueue->Send( "My Message Data." );
      }

      return;
   }

};

int main()
{
   
   // Create a new instance of the class.
   MyNewQueue^ myNewQueue = gcnew MyNewQueue;
   
   // Send a message to a queue.
   myNewQueue->SendMessage();
   return 0;
}
J#
package MyProject;

import System.*;
import System.Messaging.*;

/// <summary>
/// Provides a container class for the example.
/// </summary>
public class MyNewQueue
{
    /***************************************************/
    // Provides an entry point into the application.
    // 
    // This example sends a message to a queue.
    /***************************************************/
    public static void main(String[] args)
    {
        // Create a new instance of the class.
        MyNewQueue myNewQueue = new MyNewQueue();
        // Send a message to a queue.
        myNewQueue.SendMessage();

        return;
    } //main

    /***************************************************/
    // Sends a message to a queue.
    /***************************************************/
    public void SendMessage()
    {
        // Connect to a queue on the local computer.
        MessageQueue myQueue = new MessageQueue(".\\myQueue");
        // Send a message to the queue.
        if (myQueue.get_Transactional() == true) {
            myQueue.Send("My Message Data.", new MessageQueueTransaction());
        }
        else {
            myQueue.Send("My Message Data.");
        }

        return;
    } //SendMessage
} //MyNewQueue

The following code example sends an application-defined Order class to a queue and then receives a message from that queue.

.NET Framework Security

  • Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see .
Platforms

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
See Also

Tags :


Page view tracker