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 類別傳送至佇列,然後接收來自該佇列的訊息。

備註

使用此多載將包含 obj 參數的訊息傳送至 所參考的 MessageQueue佇列。 您傳送至佇列的物件可以是 Message 或任何 Managed 物件。 如果您傳送以外的 Message任何物件,物件就會串行化並插入訊息本文中。

如果您使用這個多載將訊息傳送至交易式佇列,訊息將會傳送至寄不出的信件佇列。 如果您想要讓訊息成為包含其他訊息之交易的一部分,請使用採用 MessageQueueTransactionMessageQueueTransactionType 做為參數的多載。

如果您未在呼叫 Send(Object)之前設定 Formatter 屬性,格式子預設為 XmlMessageFormatter

屬性 DefaultPropertiesToSend 會套用至 以外的 Message任何物件。 例如,如果您指定標籤 DefaultPropertiesToSend 或使用成員的優先順序,這些值會套用至任何訊息,其中包含應用程式傳送至佇列時,不包含類型的 Message 物件。 傳送 Message時,針對 設定的 Message 屬性值優先於 DefaultPropertiesToSend ,而訊息 Message.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

備註

使用此多載,使用 參數所定義的transaction內部交易內容,將包含 obj 參數的訊息傳送至 所MessageQueue參考的交易佇列。 您傳送至佇列的物件可以是 Message 或任何 Managed 物件。 如果您傳送以外的 Message任何物件,物件就會串行化並插入訊息本文中。

如果您使用這個多載將訊息傳送至非交易式佇列,訊息可能會傳送至寄不出的信件佇列,而不會擲回例外狀況。

如果您未在呼叫 Send(Object)之前設定 Formatter 屬性,格式子預設為 XmlMessageFormatter

屬性 DefaultPropertiesToSend 會套用至 以外的 Message任何物件。 例如,如果您指定標籤 DefaultPropertiesToSend 或使用成員的優先順序,這些值會套用至任何訊息,其中包含應用程式傳送至佇列時,不包含類型的 Message 物件。 傳送 Message時,針對 設定的 Message 屬性值優先於 DefaultPropertiesToSend ,而訊息 Message.Formatter 的 屬性優先於佇列 MessageQueue.Formatter 的 屬性。

MessageQueueTransaction 是線程 Apartment 感知,因此如果您的 Apartment 狀態為 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 或任何 Managed 物件。 如果您傳送以外的 Message任何物件,物件就會串行化並插入訊息本文中。

如果您未在呼叫 Send(Object)之前設定 Formatter 屬性,格式子預設為 XmlMessageFormatter

屬性 DefaultPropertiesToSend 會套用至 以外的 Message任何物件。 例如,如果您指定標籤 DefaultPropertiesToSend 或使用成員的優先順序,這些值會套用至任何訊息,其中包含應用程式傳送至佇列時,不包含類型的 Message 物件。 傳送 Message時,針對 設定的 Message 屬性值優先於 DefaultPropertiesToSend ,而訊息 Message.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");

備註

使用此多載將包含 obj 參數的訊息傳送至 所參考的 MessageQueue佇列。 使用此多載時,您可以指定識別訊息的字串標籤。 您傳送至佇列的物件可以是 Message、結構、資料物件或任何 Managed 物件。 如果您傳送以外的 Message任何物件,物件就會串行化並插入訊息本文中。

訊息標籤與消息佇列標籤不同,但兩者都是應用程式相依,而且對消息佇列沒有繼承意義。

如果您使用這個多載將訊息傳送至交易式佇列,訊息將會傳送至寄不出的信件佇列。 如果您想要讓訊息成為包含其他訊息之交易的一部分,請使用採用 MessageQueueTransactionMessageQueueTransactionType 做為參數的多載。

Path 必須先指定這個 MessageQueue 實例的 屬性,才能傳送訊息。 如果您未在呼叫 Send(Object)之前設定 Formatter 屬性,格式子預設為 XmlMessageFormatter

屬性 DefaultPropertiesToSend 會套用至 以外的 Message任何物件。 例如,如果您指定標籤 DefaultPropertiesToSend 或使用成員的優先順序,這些值會套用至任何訊息,其中包含應用程式傳送至佇列時,不包含類型的 Message 物件。 傳送 Message時,針對 設定的 Message 屬性值優先於 DefaultPropertiesToSend ,而訊息 Message.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();
}

備註

使用此多載,使用 參數所定義的transaction內部交易內容,將包含 obj 參數的訊息傳送至 所MessageQueue參考的交易佇列。 使用此多載時,您可以指定識別訊息的字串標籤。 您傳送至佇列的物件可以是 Message、結構、資料物件或任何 Managed 物件。 如果您傳送以外的 Message任何物件,物件就會串行化並插入訊息本文中。

訊息標籤與消息佇列標籤不同,但兩者都是應用程式相依,而且對消息佇列沒有繼承意義。

如果您使用這個多載將訊息傳送至非交易式佇列,訊息可能會傳送至寄不出的信件佇列,而不會擲回例外狀況。

如果您未在呼叫 Send(Object)之前設定 Formatter 屬性,格式子預設為 XmlMessageFormatter

屬性 DefaultPropertiesToSend 會套用至 以外的 Message任何物件。 例如,如果您指定標籤 DefaultPropertiesToSend 或使用成員的優先順序,這些值會套用至任何訊息,其中包含應用程式傳送至佇列時,不包含類型的 Message 物件。 傳送 Message時,針對 設定的 Message 屬性值優先於 DefaultPropertiesToSend ,而訊息 Message.Formatter 的屬性優先於佇列 MessageQueue.Formatter 的屬性

MessageQueueTransaction 是線程 Apartment 感知,因此如果您的 Apartment 狀態為 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 參數的訊息傳送至 所 MessageQueue參考的 transactionType 佇列。 Automatic如果已經附加至您要用來傳送訊息之線程的外部交易內容,請指定 transactionType 參數。 指定 Single 是否要將訊息傳送為單一內部交易。 您可以指定 None 是否要將交易式訊息傳送至非交易式線程。

您傳送至佇列的物件可以是 Message 或任何 Managed 物件。 如果您傳送以外的 Message任何物件,物件就會串行化並插入訊息本文中。 使用此多載時,您可以指定識別訊息的字串標籤。

訊息標籤與消息佇列標籤不同,但兩者都是應用程式相依,而且對消息佇列沒有繼承意義。

如果您未在呼叫 Send(Object)之前設定 Formatter 屬性,格式子預設為 XmlMessageFormatter

屬性 DefaultPropertiesToSend 會套用至 以外的 Message任何物件。 例如,如果您指定標籤 DefaultPropertiesToSend 或使用成員的優先順序,這些值會套用至任何訊息,其中包含應用程式傳送至佇列時,不包含類型的 Message 物件。 傳送 Message時,針對 設定的 Message 屬性值優先於 DefaultPropertiesToSend,而訊息 Message.Formatter 的 屬性優先於佇列 MessageQueue.Formatter 的 屬性。

下表顯示這個方法是否可在各種工作組模式中使用。

工作組模式 可用
本機電腦
本機計算機和直接格式名稱
遠端電腦
遠端電腦和直接格式名稱

另請參閱

適用於