MessageQueue.Send 方法

定义

向队列发送对象。

重载

Send(Object)

将对象发送到此 MessageQueue 引用的非事务性队列。

Send(Object, MessageQueueTransaction)

将对象发送到此 MessageQueue 所引用的事务性队列。

Send(Object, MessageQueueTransactionType)

将对象发送到此 MessageQueue 所引用的队列。

Send(Object, String)

将对象发送到此 MessageQueue 引用的非事务性队列,并指定消息的标签。

Send(Object, String, MessageQueueTransaction)

将对象发送到此 MessageQueue 引用的事务性队列中,并指定该消息的标签。

Send(Object, String, MessageQueueTransactionType)

将对象发送到此 MessageQueue 引用的队列中,并指定该消息的标签。

Send(Object)

将对象发送到此 MessageQueue 引用的非事务性队列。

public:
 void Send(System::Object ^ obj);
public void Send (object obj);
member this.Send : obj -> unit
Public Sub Send (obj As Object)

参数

obj
Object

要发送到队列的对象。

例外

尚未设置 Path 属性。

  • 或 - 访问“消息队列”方法时出错。

示例

下面的代码示例连接到消息队列,并将消息发送到队列。

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


        '
        ' 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

End Class

下面的代码示例将应用程序定义的 Order 类发送到队列,然后从该队列接收消息。

注解

Use this overload to send a message that contains the obj parameter to the queue referenced by the MessageQueue. 发送到队列的对象可以是一个或多个 Message 托管对象。 如果发送除某个 Message对象以外的任何对象,则会序列化对象并将其插入到消息正文中。

如果使用此重载将消息发送到事务队列,则会将消息发送到死信队列。 如果希望消息是包含其他消息的事务的一部分,请使用采用 MessageQueueTransactionMessageQueueTransactionType 作为参数的重载。

If you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

DefaultPropertiesToSend 属性适用于除 . Message. 以外的任何对象。 例如,如果指定了使用成员的标签或优先级 DefaultPropertiesToSend ,则这些值将应用于应用程序将对象发送到队列时不包含类型 Message 的任何消息。 发送时 Message,为 Message 优先级设置 DefaultPropertiesToSendMessage.Formatter 属性值,并且消息的属性优先于队列 MessageQueue.Formatter 的属性。

下表显示了此方法是否在各种工作组模式下可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

Send(Object, MessageQueueTransaction)

将对象发送到此 MessageQueue 所引用的事务性队列。

public:
 void Send(System::Object ^ obj, System::Messaging::MessageQueueTransaction ^ transaction);
public void Send (object obj, System.Messaging.MessageQueueTransaction transaction);
member this.Send : obj * System.Messaging.MessageQueueTransaction -> unit
Public Sub Send (obj As Object, transaction As MessageQueueTransaction)

参数

obj
Object

要发送到队列的对象。

例外

transaction 参数为 null

尚未设置 Path 属性。

  • 或 - “消息队列”应用程序指示事务用法不正确。

  • 或 - 访问“消息队列”方法时出错。

示例

下面的代码示例将字符串发送到事务队列,然后从该队列接收消息。

#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;
}
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;
        }
    }
}
Imports System.Messaging

   
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


        '
        ' 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
                ' 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()
            End If

            Return

        End Sub


        '
        ' 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

End Class

注解

使用此重载使用参数定义的内部事务上下文将包含 obj 参数的消息发送到由 MessageQueue该参数引用的 transaction 事务队列。 发送到队列的对象可以是一个或多个 Message 托管对象。 如果发送除某个 Message对象以外的任何对象,则会序列化对象并将其插入到消息正文中。

如果使用此重载将消息发送到非事务性队列,则消息可能会发送到死信队列,而不会引发异常。

If you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

DefaultPropertiesToSend 属性适用于除 . Message. 以外的任何对象。 例如,如果指定了使用成员的标签或优先级 DefaultPropertiesToSend ,则这些值将应用于应用程序将对象发送到队列时不包含类型 Message 的任何消息。 发送时 Message,为 Message 优先级设置 DefaultPropertiesToSendMessage.Formatter 属性值,并且消息的属性优先于队列 MessageQueue.Formatter 的属性。

MessageQueueTransaction 线程单元感知,因此,如果单元状态为 STA,则不能在多个线程中使用事务。 Visual Basic将主线程的状态设置为STA,因此必须在子例程中Main应用MTAThreadAttribute该状态。 否则,利用另一个线程发送事务性消息将引发 MessageQueueException 异常。 使用以下片段应用 MTAThreadAttribute 该代码。

<System.MTAThreadAttribute>  
 public sub Main()  

下表显示了此方法是否在各种工作组模式下可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

Send(Object, MessageQueueTransactionType)

将对象发送到此 MessageQueue 所引用的队列。

public:
 void Send(System::Object ^ obj, System::Messaging::MessageQueueTransactionType transactionType);
public void Send (object obj, System.Messaging.MessageQueueTransactionType transactionType);
member this.Send : obj * System.Messaging.MessageQueueTransactionType -> unit
Public Sub Send (obj As Object, transactionType As MessageQueueTransactionType)

参数

obj
Object

要发送到队列的对象。

transactionType
MessageQueueTransactionType

MessageQueueTransactionType 值之一,它描述与消息关联的事务上下文的类型。

例外

transactionType 参数不是 MessageQueueTransactionType 成员之一。

尚未设置 Path 属性。

  • 或 - 访问“消息队列”方法时出错。

示例

以下代码示例演示了 Send(Object, MessageQueueTransactionType) 的用法。


// Connect to a transactional queue on the local computer.
MessageQueue^ queue = gcnew MessageQueue(".\\exampleTransQueue");

// Create a new message.
Message^ msg = gcnew Message("Example Message Body");

// Send the message.
queue->Send(msg, MessageQueueTransactionType::Single);

queue->Close();

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

// Create a new message.
Message msg = new Message("Example Message Body");

// Send the message.
queue.Send(msg, MessageQueueTransactionType.Single);

注解

使用此重载使用参数定义的事务上下文将包含 obj 参数的消息发送到由 MessageQueue参数引用的 transactionType 队列。 Automatic如果已将外部事务上下文附加到要用于发送消息的线程,请指定transactionType该参数。 指定 Single 是否要将消息作为单个内部事务发送。 可以指定 None 是否要将事务消息发送到非事务线程。

发送到队列的对象可以是一个或多个 Message 托管对象。 如果发送除某个 Message对象以外的任何对象,则会序列化对象并将其插入到消息正文中。

If you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

DefaultPropertiesToSend 属性适用于除 . Message. 以外的任何对象。 例如,如果指定了使用成员的标签或优先级 DefaultPropertiesToSend ,则这些值将应用于应用程序将对象发送到队列时不包含类型 Message 的任何消息。 发送时 Message,为 Message 优先级设置 DefaultPropertiesToSendMessage.Formatter 属性值,并且消息的属性优先于队列 MessageQueue.Formatter 的属性。

下表显示了此方法是否在各种工作组模式下可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

Send(Object, String)

将对象发送到此 MessageQueue 引用的非事务性队列,并指定消息的标签。

public:
 void Send(System::Object ^ obj, System::String ^ label);
public void Send (object obj, string label);
member this.Send : obj * string -> unit
Public Sub Send (obj As Object, label As String)

参数

obj
Object

要发送到队列的对象。

label
String

消息的标签。

例外

label 参数为 null

尚未设置 Path 属性。

  • 或 - 访问“消息队列”方法时出错。

示例

以下代码示例演示了 Send(Object, String) 的用法。


// Connect to a queue on the local computer.
MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue");

// Create a new message.
Message^ msg = gcnew Message("Example Message Body");

// Send the message.
queue->Send(msg, "Example Message Label");

queue->Close();

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

// Create a new message.
Message msg = new Message("Example Message Body");

// Send the message.
queue.Send(msg, "Example Message Label");

注解

Use this overload to send a message that contains the obj parameter to the queue referenced by the MessageQueue. 使用此重载,可以指定标识消息的字符串标签。 发送到队列的对象可以是结构 Message、数据对象或任何托管对象。 如果发送除某个 Message对象以外的任何对象,则会序列化对象并将其插入到消息正文中。

消息标签不同于消息队列标签,但两者都依赖于应用程序,并且对消息队列没有继承意义。

如果使用此重载将消息发送到事务队列,则会将消息发送到死信队列。 如果希望消息是包含其他消息的事务的一部分,请使用采用 MessageQueueTransactionMessageQueueTransactionType 作为参数的重载。

Path在发送消息之前,必须指定此MessageQueue实例的属性。 If you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

DefaultPropertiesToSend 属性适用于除 . Message. 以外的任何对象。 例如,如果指定了使用成员的标签或优先级 DefaultPropertiesToSend ,则这些值将应用于应用程序将对象发送到队列时不包含类型 Message 的任何消息。 发送时 Message,为 Message 优先级设置 DefaultPropertiesToSendMessage.Formatter 属性值,并且消息的属性优先于队列 MessageQueue.Formatter 的属性。

下表显示了此方法是否在各种工作组模式下可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

Send(Object, String, MessageQueueTransaction)

将对象发送到此 MessageQueue 引用的事务性队列中,并指定该消息的标签。

public:
 void Send(System::Object ^ obj, System::String ^ label, System::Messaging::MessageQueueTransaction ^ transaction);
public void Send (object obj, string label, System.Messaging.MessageQueueTransaction transaction);
member this.Send : obj * string * System.Messaging.MessageQueueTransaction -> unit
Public Sub Send (obj As Object, label As String, transaction As MessageQueueTransaction)

参数

obj
Object

要发送到队列的对象。

label
String

消息的标签。

例外

label 参数为 null

  • 或 - transaction 参数为 null

尚未设置 Path 属性。

  • 或 - “消息队列”应用程序指示了不正确的事务用法。

  • 或 - 访问“消息队列”方法时出错。

示例

以下代码示例演示了 Send(Object, String, MessageQueueTransaction) 的用法。


// Connect to a transactional queue on the local computer.
MessageQueue^ queue = gcnew MessageQueue(".\\exampleTransQueue");

// Create a new message.
Message^ msg = gcnew Message("Example Message Body");

// Create a message queuing transaction.
MessageQueueTransaction^ transaction = gcnew MessageQueueTransaction();

try
{
    // Begin a transaction.
    transaction->Begin();

    // Send the message to the queue.
    queue->Send(msg, "Example Message Label", transaction);

    // Commit the transaction.
    transaction->Commit();
}
catch (Exception^ ex)
{
    // Cancel the transaction.
    transaction->Abort();

    // Propagate the exception.
    throw ex;
}
finally
{
    // Dispose of the transaction object.
    delete transaction;
    queue->Close();
}

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

// Create a new message.
Message msg = new Message("Example Message Body");

// Create a message queuing transaction.
MessageQueueTransaction transaction = new MessageQueueTransaction();

try
{
    // Begin a transaction.
    transaction.Begin();

    // Send the message to the queue.
    queue.Send(msg, "Example Message Label", transaction);

    // Commit the transaction.
    transaction.Commit();
}
catch(System.Exception e)
{
    // Cancel the transaction.
    transaction.Abort();

    // Propagate the exception.
    throw e;
}
finally
{
    // Dispose of the transaction object.
    transaction.Dispose();
}

注解

使用此重载使用参数定义的内部事务上下文将包含 obj 参数的消息发送到由 MessageQueue该参数引用的 transaction 事务队列。 使用此重载,可以指定标识消息的字符串标签。 发送到队列的对象可以是结构 Message、数据对象或任何托管对象。 如果发送除某个 Message对象以外的任何对象,则会序列化对象并将其插入到消息正文中。

消息标签不同于消息队列标签,但两者都依赖于应用程序,并且对消息队列没有继承意义。

如果使用此重载将消息发送到非事务性队列,则消息可能会发送到死信队列,而不会引发异常。

If you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

DefaultPropertiesToSend 属性适用于除 . Message. 以外的任何对象。 例如,如果指定了使用成员的标签或优先级 DefaultPropertiesToSend ,则这些值将应用于应用程序将对象发送到队列时不包含类型 Message 的任何消息。 发送时 Message,为 Message 优先顺序 DefaultPropertiesToSend 设置的 Message.Formatter 属性值,并且消息的属性优先于队列 MessageQueue.Formatter 的属性

MessageQueueTransaction 线程单元感知,因此,如果单元状态为 STA,则不能在多个线程中使用事务。 Visual Basic将主线程的状态设置为STA,因此必须在子例程中Main应用MTAThreadAttribute该状态。 否则,利用另一个线程发送事务性消息将引发 MessageQueueException 异常。 使用以下片段应用 MTAThreadAttribute 该代码。

<System.MTAThreadAttribute>  
 public sub Main()  

下表显示了此方法是否在各种工作组模式下可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

Send(Object, String, MessageQueueTransactionType)

将对象发送到此 MessageQueue 引用的队列中,并指定该消息的标签。

public:
 void Send(System::Object ^ obj, System::String ^ label, System::Messaging::MessageQueueTransactionType transactionType);
public void Send (object obj, string label, System.Messaging.MessageQueueTransactionType transactionType);
member this.Send : obj * string * System.Messaging.MessageQueueTransactionType -> unit
Public Sub Send (obj As Object, label As String, transactionType As MessageQueueTransactionType)

参数

obj
Object

要发送到队列的对象。

label
String

消息的标签。

transactionType
MessageQueueTransactionType

MessageQueueTransactionType 值之一,它描述与消息关联的事务上下文的类型。

例外

label 参数为 null

“消息队列”应用程序指示了不正确的事务用法。

transactionType 参数不是 MessageQueueTransactionType 成员之一。

尚未设置 Path 属性。

  • 或 - 访问“消息队列”方法时出错。

示例

以下代码示例演示了 Send(Object, String, MessageQueueTransactionType) 的用法。


// Connect to a transactional queue on the local computer.
MessageQueue^ queue = gcnew MessageQueue(".\\exampleTransQueue");

// Create a new message.
Message^ msg = gcnew Message("Example Message Body");

// Send the message.
queue->Send(msg, "Example Message Label",
    MessageQueueTransactionType::Single);

queue->Close();

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

// Create a new message.
Message msg = new Message("Example Message Body");

// Send the message.
queue.Send(msg, "Example Message Label",
    MessageQueueTransactionType.Single);

注解

使用此重载,使用参数定义的事务上下文将包含 obj 参数的消息发送到由该队列引用 MessageQueuetransactionType 队列。 Automatic transactionType如果已将外部事务上下文附加到要用于发送消息的线程,请指定该参数。 指定 Single 是否要将消息作为单个内部事务发送。 可以指定 None 是否要将事务消息发送到非事务线程。

发送到队列的对象可以是一个或多个 Message 托管对象。 如果发送除某个 Message对象以外的任何对象,则会序列化该对象并将其插入到消息正文中。 使用此重载,可以指定标识消息的字符串标签。

消息标签不同于消息队列标签,但两者都依赖于应用程序,对消息队列没有继承意义。

If you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

DefaultPropertiesToSend 属性适用于除 . Message以外的任何对象。 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则这些值将应用于包含对象的任何消息,当应用程序将其发送到队列时,该消息的类型不是类型 Message 。 发送时 Message,为 Message 优先顺序 DefaultPropertiesToSend设置的属性值,并且消息 Message.Formatter 的属性优先于队列 MessageQueue.Formatter 的属性。

下表显示了此方法是否在各种工作组模式下可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于