MessageQueue.Send 方法

定义

向队列发送对象。Sends an object to a queue.

重载

Send(Object)

将对象发送到此 MessageQueue 引用的非事务性队列。Sends an object to non-transactional queue referenced by this MessageQueue.

Send(Object, MessageQueueTransaction)

将对象发送到此 MessageQueue 所引用的事务性队列。Sends an object to the transactional queue referenced by this MessageQueue.

Send(Object, MessageQueueTransactionType)

将对象发送到此 MessageQueue 所引用的队列。Sends an object to the queue referenced by this MessageQueue.

Send(Object, String)

将对象发送到此 MessageQueue 引用的非事务性队列,并指定消息的标签。Sends an object to the non-transactional queue referenced by this MessageQueue and specifies a label for the message.

Send(Object, String, MessageQueueTransaction)

将对象发送到此 MessageQueue 引用的事务性队列中,并指定该消息的标签。Sends an object to the transactional queue referenced by this MessageQueue and specifies a label for the message.

Send(Object, String, MessageQueueTransactionType)

将对象发送到此 MessageQueue 引用的队列中,并指定该消息的标签。Sends an object to the queue referenced by this MessageQueue and specifies a label for the message.

Send(Object)

将对象发送到此 MessageQueue 引用的非事务性队列。Sends an object to non-transactional queue referenced by this 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

要发送到队列的对象。The object to send to the queue.

例外

尚未设置 Path 属性。The Path property has not been set.

- 或 --or- 访问“消息队列”方法时出错。An error occurred when accessing a Message Queuing method.

示例

下面的代码示例连接到消息队列并将消息发送到队列。The following code example connects to a message queue and sends a message to the queue.

#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 类发送到队列,然后从该队列接收消息。The following code example sends an application-defined Order class to a queue and then receives a message from that queue.

注解

使用此重载将包含参数的消息发送 obj 到由引用的队列 MessageQueueUse this overload to send a message that contains the obj parameter to the queue referenced by the MessageQueue. 发送到队列的对象可以为 Message 或任何托管对象。The object you send to the queue can be a Message or any managed object. 如果发送除之外的任何对象 Message ,则会序列化对象并将其插入消息的正文中。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. 如果希望消息成为包含其他消息的事务的一部分,请使用采用 MessageQueueTransactionMessageQueueTransactionType 作为参数的重载。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.

如果在调用之前未设置 Formatter 属性 Send(Object) ,则格式化程序默认为 XmlMessageFormatterIf you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

DefaultPropertiesToSend属性应用于以外的任何对象 MessageThe DefaultPropertiesToSend property applies to any object other than a Message. 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则 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. 在发送时 Message ,为设置的属性值 Message 优先于, DefaultPropertiesToSend 并且消息的 Message.Formatter 属性优先于队列的 MessageQueue.Formatter 属性。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

另请参阅

适用于

Send(Object, MessageQueueTransaction)

将对象发送到此 MessageQueue 所引用的事务性队列。Sends an object to the transactional queue referenced by this 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

要发送到队列的对象。The object to send to the queue.

例外

transaction 参数为 nullThe transaction parameter is null.

尚未设置 Path 属性。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.

示例

下面的代码示例将字符串发送到事务性队列,然后从该队列接收消息。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;
}
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 ,使用由参数定义的内部事务上下文将包含参数的消息发送到引用的事务性队列 transactionUse 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. 发送到队列的对象可以为 Message 或任何托管对象。The object you send to the queue can be a Message or any managed object. 如果发送除之外的任何对象 Message ,则会序列化对象并将其插入消息的正文中。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.

如果在调用之前未设置 Formatter 属性 Send(Object) ,则格式化程序默认为 XmlMessageFormatterIf you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

DefaultPropertiesToSend属性应用于以外的任何对象 MessageThe DefaultPropertiesToSend property applies to any object other than a Message. 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则 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. 在发送时 Message ,为设置的属性值 Message 优先于, DefaultPropertiesToSend 并且消息的 Message.Formatter 属性优先于队列的 MessageQueue.Formatter 属性。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.

MessageQueueTransaction 线程单元感知,因此,如果单元状态为 STA ,则不能在多个线程中使用事务。MessageQueueTransaction is threading apartment aware, so if your apartment state is STA, you cannot use the transaction in multiple threads. Visual Basic 将主线程的状态设置为 STA ,因此你必须 MTAThreadAttribute 在子例程中应用 MainVisual Basic sets the state of the main thread to STA, so you must apply the MTAThreadAttribute in the Main subroutine. 否则,利用另一个线程发送事务性消息将引发 MessageQueueException 异常。Otherwise, sending a transactional message using another thread throws a MessageQueueException exception. MTAThreadAttribute使用以下片段应用。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

另请参阅

适用于

Send(Object, MessageQueueTransactionType)

将对象发送到此 MessageQueue 所引用的队列。Sends an object to the queue referenced by this 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

要发送到队列的对象。The object to send to the queue.

transactionType
MessageQueueTransactionType

MessageQueueTransactionType 值之一,它描述与消息关联的事务上下文的类型。One of the MessageQueueTransactionType values, describing the type of transaction context to associate with the message.

例外

transactionType 参数不是 MessageQueueTransactionType 成员之一。The transactionType parameter is not one of the MessageQueueTransactionType members.

尚未设置 Path 属性。The Path property has not been set.

- 或 --or- 访问“消息队列”方法时出错。An error occurred when accessing a Message Queuing method.

示例

以下代码示例演示了 Send(Object, MessageQueueTransactionType) 的用法。The following code example demonstrates the use of 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 ,使用由参数定义的事务上下文 transactionTypeUse this overload to send a message that contains the obj parameter to the queue referenced by the MessageQueue, using a transaction context defined by the transactionType parameter. Automatic transactionType 如果已存在附加到要用于发送消息的线程的外部事务上下文,请为参数指定。Specify Automatic for the transactionType parameter if there is already an external transaction context attached to the thread that you want to use to send the message. 指定 Single 是否要将消息作为单个内部事务发送。Specify Single if you want to send the message as a single internal transaction. 您可以指定 None 是否要将事务性消息发送到非事务性线程。You can specify None if you want to send a transactional message to a non-transactional thread.

发送到队列的对象可以为 Message 或任何托管对象。The object you send to the queue can be a Message or any managed object. 如果发送除之外的任何对象 Message ,则会序列化对象并将其插入消息的正文中。If you send any object other than a Message, the object is serialized and inserted into the body of the message.

如果在调用之前未设置 Formatter 属性 Send(Object) ,则格式化程序默认为 XmlMessageFormatterIf you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

DefaultPropertiesToSend属性应用于以外的任何对象 MessageThe DefaultPropertiesToSend property applies to any object other than a Message. 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则 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. 在发送时 Message ,为设置的属性值 Message 优先于, DefaultPropertiesToSend 并且消息的 Message.Formatter 属性优先于队列的 MessageQueue.Formatter 属性。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

另请参阅

适用于

Send(Object, String)

将对象发送到此 MessageQueue 引用的非事务性队列,并指定消息的标签。Sends an object to the non-transactional queue referenced by this MessageQueue and specifies a label for the message.

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

要发送到队列的对象。The object to send to the queue.

label
String

消息的标签。The label of the message.

例外

label 参数为 nullThe label parameter is null.

尚未设置 Path 属性。The Path property has not been set.

- 或 --or- 访问“消息队列”方法时出错。An error occurred when accessing a Message Queuing method.

示例

以下代码示例演示了 Send(Object, String) 的用法。The following code example demonstrates the use of 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");

注解

使用此重载将包含参数的消息发送 obj 到由引用的队列 MessageQueueUse this overload to send a message that contains the obj parameter to the queue referenced by the MessageQueue. 利用此重载,可以指定标识消息的字符串标签。With this overload, you can specify the string label that identifies the message. 发送到队列的对象可以是 Message 、结构、数据对象或任何托管对象。The object you send to the queue can be a Message, a structure, a data object, or any managed object. 如果发送除之外的任何对象 Message ,则会序列化对象并将其插入消息的正文中。If you send any object other than a Message, the object is serialized and inserted into the body of the message.

消息标签不同于消息队列标签,但两者都依赖于应用程序,并且对消息队列没有继承意义。The message label is distinct from the message queue label, but both are application-dependent and have no inherit meaning to Message Queuing.

如果使用此重载将消息发送到事务性队列,则会将该消息发送到死信队列。If you use this overload to send a message to a transactional queue, the message will be sent to the dead-letter queue. 如果希望消息成为包含其他消息的事务的一部分,请使用采用 MessageQueueTransactionMessageQueueTransactionType 作为参数的重载。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.

Path MessageQueue 必须先指定此实例的属性,然后才能发送消息。The Path property for this MessageQueue instance must be specified before you send the message. 如果在调用之前未设置 Formatter 属性 Send(Object) ,则格式化程序默认为 XmlMessageFormatterIf you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

DefaultPropertiesToSend属性应用于以外的任何对象 MessageThe DefaultPropertiesToSend property applies to any object other than a Message. 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则 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. 在发送时 Message ,为设置的属性值 Message 优先于, DefaultPropertiesToSend 并且消息的 Message.Formatter 属性优先于队列的 MessageQueue.Formatter 属性。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

另请参阅

适用于

Send(Object, String, MessageQueueTransaction)

将对象发送到此 MessageQueue 引用的事务性队列中,并指定该消息的标签。Sends an object to the transactional queue referenced by this MessageQueue and specifies a label for the message.

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

要发送到队列的对象。The object to send to the queue.

label
String

消息的标签。The label of the message.

例外

label 参数为 nullThe label parameter is null.

- 或 --or- transaction 参数为 nullThe transaction parameter is null.

尚未设置 Path 属性。The Path property has not been set.

- 或 --or- “消息队列”应用程序指示了不正确的事务用法。The Message Queuing application indicated an incorrect transaction usage.

- 或 --or- 访问“消息队列”方法时出错。An error occurred when accessing a Message Queuing method.

示例

以下代码示例演示了 Send(Object, String, MessageQueueTransaction) 的用法。The following code example demonstrates the use of 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 ,使用由参数定义的内部事务上下文将包含参数的消息发送到引用的事务性队列 transactionUse 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. 利用此重载,可以指定标识消息的字符串标签。With this overload, you can specify the string label that identifies the message. 发送到队列的对象可以是 Message 、结构、数据对象或任何托管对象。The object you send to the queue can be a Message, a structure, a data object, or any managed object. 如果发送除之外的任何对象 Message ,则会序列化对象并将其插入消息的正文中。If you send any object other than a Message, the object is serialized and inserted into the body of the message.

消息标签不同于消息队列标签,但两者都依赖于应用程序,并且对消息队列没有继承意义。The message label is distinct from the message queue label, but both are application-dependent and have no inherit meaning to Message Queuing.

如果使用此重载将消息发送到非事务性队列,则可能会将该消息发送到死信队列,而不会引发异常。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.

如果在调用之前未设置 Formatter 属性 Send(Object) ,则格式化程序默认为 XmlMessageFormatterIf you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

DefaultPropertiesToSend属性应用于以外的任何对象 MessageThe DefaultPropertiesToSend property applies to any object other than a Message. 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则 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. 在发送时 Message ,为设置的属性值 Message 优先于, DefaultPropertiesToSend 并且消息的 Message.Formatter 属性优先于队列的 MessageQueue.Formatter 属性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

MessageQueueTransaction 线程单元感知,因此,如果单元状态为 STA ,则不能在多个线程中使用事务。MessageQueueTransaction is threading apartment aware, so if your apartment state is STA, you cannot use the transaction in multiple threads. Visual Basic 将主线程的状态设置为 STA ,因此你必须 MTAThreadAttribute 在子例程中应用 MainVisual Basic sets the state of the main thread to STA, so you must apply the MTAThreadAttribute in the Main subroutine. 否则,利用另一个线程发送事务性消息将引发 MessageQueueException 异常。Otherwise, sending a transactional message using another thread throws a MessageQueueException exception. MTAThreadAttribute使用以下片段应用。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

另请参阅

适用于

Send(Object, String, MessageQueueTransactionType)

将对象发送到此 MessageQueue 引用的队列中,并指定该消息的标签。Sends an object to the queue referenced by this MessageQueue and specifies a label for the message.

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

要发送到队列的对象。The object to send to the queue.

label
String

消息的标签。The label of the message.

transactionType
MessageQueueTransactionType

MessageQueueTransactionType 值之一,它描述与消息关联的事务上下文的类型。One of the MessageQueueTransactionType values, describing the type of transaction context to associate with the message.

例外

label 参数为 nullThe label parameter is null.

“消息队列”应用程序指示了不正确的事务用法。The Message Queuing application indicated an incorrect transaction usage.

transactionType 参数不是 MessageQueueTransactionType 成员之一。The transactionType parameter is not one of the MessageQueueTransactionType members.

尚未设置 Path 属性。The Path property has not been set.

- 或 --or- 访问“消息队列”方法时出错。An error occurred when accessing a Message Queuing method.

示例

以下代码示例演示了 Send(Object, String, MessageQueueTransactionType) 的用法。The following code example demonstrates the use of 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 到由引用的队列 MessageQueue ,使用由参数定义的事务上下文 transactionTypeUse this overload to send a message that contains the obj parameter to the queue referenced by the MessageQueue, using a transaction context defined by the transactionType parameter. Automatic transactionType 如果已存在附加到要用于发送消息的线程的外部事务上下文,请为参数指定。Specify Automatic for the transactionType parameter if there is already an external transaction context attached to the thread that you want to use to send the message. 指定 Single 是否要将消息作为单个内部事务发送。Specify Single if you want to send the message as a single internal transaction. 您可以指定 None 是否要将事务性消息发送到非事务性线程。You can specify None if you want to send a transactional message to a non-transactional thread.

发送到队列的对象可以为 Message 或任何托管对象。The object you send to the queue can be a Message or any managed object. 如果发送除之外的任何对象 Message ,则会序列化对象并将其插入消息的正文中。If you send any object other than a Message, the object is serialized and inserted into the body of the message. 利用此重载,可以指定标识消息的字符串标签。With this overload, you can specify the string label that identifies the message.

消息标签不同于消息队列标签,但两者都依赖于应用程序,并且对消息队列没有继承意义。The message label is distinct from the message queue label, but both are application-dependent and have no inherit meaning to Message Queuing.

如果在调用之前未设置 Formatter 属性 Send(Object) ,则格式化程序默认为 XmlMessageFormatterIf you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

DefaultPropertiesToSend属性应用于以外的任何对象 MessageThe DefaultPropertiesToSend property applies to any object other than a Message. 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则 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. 在发送时 Message ,为设置的属性值 Message 优先于 DefaultPropertiesToSend ,并且消息的 Message.Formatter 属性优先于队列的 MessageQueue.Formatter 属性。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

另请参阅

适用于