MessageQueue.Send 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將物件傳送到隊列。
多載
| 名稱 | Description |
|---|---|
| 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
要送入佇列的物件。
例外狀況
範例
以下程式碼範例連接訊息佇列並向佇列發送訊息。
#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 )
{
// 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)
{
// 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參考的佇列。 你送到佇列的物件可以是 A Message 或任何受管理物件。 如果你發送除了 的 Message物件,該物件會被序列化並插入訊息正文中。
如果你用這個超載將訊息送到交易佇列,該訊息就會被送到死信佇列。 如果你希望訊息成為包含其他訊息的交易的一部分,可以使用以 MessageQueueTransaction or MessageQueueTransactionType 作為參數的超載。
若在呼叫Send(Object)前未設定該Formatter屬性,格式化器預設為 XmlMessageFormatter。
此 DefaultPropertiesToSend 性質適用於除 Message。 例如,如果你用成員指定標籤或優先權 DefaultPropertiesToSend ,這些值會適用於任何包含物件且在應用程式送入隊列時非型別 Message 物件的訊息。 當傳送 時 Message,設定的 Message 屬性值優先於 DefaultPropertiesToSend ,訊息的 Message.Formatter 屬性優先於佇列屬性 MessageQueue.Formatter 。
下表顯示此方法是否可在多種工作群組模式中使用。
| 工作群組模式 | 有現貨 |
|---|---|
| 本機電腦 | 是的 |
| 本地電腦與直接格式名稱 | 是的 |
| 遠端電腦 | No |
| 遠端電腦與直接格式名稱 | 是的 |
另請參閱
適用於
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
- MessageQueueTransaction
例外狀況
參數 transaction 為 null。
範例
以下程式碼範例將一個字串傳送到交易佇列,然後從該佇列接收訊息。
#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 )
{
// 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)
{
// 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訊息傳送到由參數定義transaction的內部交易上下文,將包含參數所參考MessageQueue的交易佇列。 你送到佇列的物件可以是 A Message 或任何受管理物件。 如果你發送除了 的 Message物件,該物件會被序列化並插入訊息正文中。
如果你利用這個超載方式將訊息傳送到非交易佇列,訊息可能會被送往死單佇列,而不會拋出例外。
若在呼叫Send(Object)前未設定該Formatter屬性,格式化器預設為 XmlMessageFormatter。
此 DefaultPropertiesToSend 性質適用於除 Message。 例如,如果你用成員指定標籤或優先權 DefaultPropertiesToSend ,這些值會適用於任何包含物件且在應用程式送入隊列時非型別 Message 物件的訊息。 當傳送 時 Message,設定的 Message 屬性值優先於 DefaultPropertiesToSend ,訊息的 Message.Formatter 屬性優先於佇列屬性 MessageQueue.Formatter 。
MessageQueueTransaction 是 Threading Apartment 的 Ware,所以如果你的 Apartment 狀態是 STA,你就不能在多個執行緒中使用交易。 Visual Basic 將主執行緒的狀態設為 STA,因此你必須在子程式中Main套用 。MTAThreadAttribute 否則,使用另一執行緒傳送交易訊息會拋 MessageQueueException 出例外。 你用以下片段來應用。MTAThreadAttribute
<System.MTAThreadAttribute>
public sub Main()
下表顯示此方法是否可在多種工作群組模式中使用。
| 工作群組模式 | 有現貨 |
|---|---|
| 本機電腦 | 是的 |
| 本地電腦與直接格式名稱 | 是的 |
| 遠端電腦 | No |
| 遠端電腦與直接格式名稱 | 是的 |
另請參閱
- DefaultPropertiesToSend
- Message
- MessageQueueTransaction
- Transactional
- Peek()
- Receive()
- BeginPeek()
- BeginReceive()
適用於
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 成員之一。
範例
下列程式代碼範例示範 如何使用 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訊息傳送到由參數定義transactionType的交易上下文,傳送包含參數的MessageQueue佇列訊息。 如果你想用來傳送訊息的執行緒已經附加了外部交易上下文,請指定AutomatictransactionType參數。 請指定 Single 是否要將訊息作為單一內部交易發送。 你可以指定 None 是否要將交易訊息傳送到非交易執行緒。
你送到佇列的物件可以是 A Message 或任何受管理物件。 如果你發送除了 的 Message物件,該物件會被序列化並插入訊息正文中。
若在呼叫Send(Object)前未設定該Formatter屬性,格式化器預設為 XmlMessageFormatter。
此 DefaultPropertiesToSend 性質適用於除 Message。 例如,如果你用成員指定標籤或優先權 DefaultPropertiesToSend ,這些值會適用於任何包含物件且在應用程式送入隊列時非型別 Message 物件的訊息。 當傳送 時 Message,設定的 Message 屬性值優先於 DefaultPropertiesToSend ,訊息的 Message.Formatter 屬性優先於佇列屬性 MessageQueue.Formatter 。
下表顯示此方法是否可在多種工作群組模式中使用。
| 工作群組模式 | 有現貨 |
|---|---|
| 本機電腦 | 是的 |
| 本地電腦與直接格式名稱 | 是的 |
| 遠端電腦 | No |
| 遠端電腦與直接格式名稱 | 是的 |
另請參閱
- MessageQueueTransactionType
- DefaultPropertiesToSend
- Message
- Transactional
- Peek()
- Receive()
- BeginPeek()
- BeginReceive()
適用於
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。
範例
下列程式代碼範例示範 如何使用 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、結構體、資料物件或任何受管理物件。 如果你發送除了 的 Message物件,該物件會被序列化並插入訊息正文中。
訊息標籤與訊息佇列標籤不同,但兩者皆依賴應用程式,且對訊息排隊本身沒有任何意義。
如果你用這個超載將訊息送到交易佇列,該訊息就會被送到死信佇列。 如果你希望訊息成為包含其他訊息的交易的一部分,可以使用以 MessageQueueTransaction or MessageQueueTransactionType 作為參數的超載。
Path此實例的屬性MessageQueue必須在發送訊息前指定。 若在呼叫Send(Object)前未設定該Formatter屬性,格式化器預設為 XmlMessageFormatter。
此 DefaultPropertiesToSend 性質適用於除 Message。 例如,如果你用成員指定標籤或優先權 DefaultPropertiesToSend ,這些值會適用於任何包含物件且在應用程式送入隊列時非型別 Message 物件的訊息。 當傳送 時 Message,設定的 Message 屬性值優先於 DefaultPropertiesToSend ,訊息的 Message.Formatter 屬性優先於佇列屬性 MessageQueue.Formatter 。
下表顯示此方法是否可在多種工作群組模式中使用。
| 工作群組模式 | 有現貨 |
|---|---|
| 本機電腦 | 是的 |
| 本地電腦與直接格式名稱 | 是的 |
| 遠端電腦 | No |
| 遠端電腦與直接格式名稱 | 是的 |
另請參閱
適用於
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
訊息的標籤。
- transaction
- MessageQueueTransaction
例外狀況
範例
下列程式代碼範例示範 如何使用 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訊息傳送到由參數定義transaction的內部交易上下文,將包含參數所參考MessageQueue的交易佇列。 有了這個超載,你可以指定用來識別訊息的字串標籤。 你送給佇列的物件可以是 Message、結構體、資料物件或任何受管理物件。 如果你發送除了 的 Message物件,該物件會被序列化並插入訊息正文中。
訊息標籤與訊息佇列標籤不同,但兩者皆依賴應用程式,且對訊息排隊本身沒有任何意義。
如果你利用這個超載方式將訊息傳送到非交易佇列,訊息可能會被送往死單佇列,而不會拋出例外。
若在呼叫Send(Object)前未設定該Formatter屬性,格式化器預設為 XmlMessageFormatter。
此 DefaultPropertiesToSend 性質適用於除 Message。 例如,如果你用成員指定標籤或優先權 DefaultPropertiesToSend ,這些值會適用於任何包含物件且在應用程式送入隊列時非型別 Message 物件的訊息。 當傳送 時 Message,設定的 Message 屬性值優先 DefaultPropertiesToSend 於 ,訊息的 Message.Formatter 屬性優先於佇列的 MessageQueue.Formatter 屬性
MessageQueueTransaction 是 Threading Apartment 的 Ware,所以如果你的 Apartment 狀態是 STA,你就不能在多個執行緒中使用交易。 Visual Basic 將主執行緒的狀態設為 STA,因此你必須在子程式中Main套用 。MTAThreadAttribute 否則,使用另一執行緒傳送交易訊息會拋 MessageQueueException 出例外。 你用以下片段來應用。MTAThreadAttribute
<System.MTAThreadAttribute>
public sub Main()
下表顯示此方法是否可在多種工作群組模式中使用。
| 工作群組模式 | 有現貨 |
|---|---|
| 本機電腦 | 是的 |
| 本地電腦與直接格式名稱 | 是的 |
| 遠端電腦 | No |
| 遠端電腦與直接格式名稱 | 是的 |
另請參閱
- DefaultPropertiesToSend
- Message
- MessageQueueTransaction
- Transactional
- Label
- Peek()
- Receive()
- BeginPeek()
- BeginReceive()
適用於
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 成員之一。
範例
下列程式代碼範例示範 如何使用 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訊息傳送到由參數定義transactionType的交易上下文,傳送包含參數的MessageQueue佇列訊息。 如果你想用來傳送訊息的執行緒已經附加了外部交易上下文,請指定AutomatictransactionType參數。 請指定 Single 是否要將訊息作為單一內部交易發送。 你可以指定 None 是否要將交易訊息傳送到非交易執行緒。
你送到佇列的物件可以是 A Message 或任何受管理物件。 如果你發送除了 的 Message物件,該物件會被序列化並插入訊息正文中。 有了這個超載,你可以指定用來識別訊息的字串標籤。
訊息標籤與訊息佇列標籤不同,但兩者皆依賴應用程式,且對訊息排隊本身沒有任何意義。
若在呼叫Send(Object)前未設定該Formatter屬性,格式化器預設為 XmlMessageFormatter。
此 DefaultPropertiesToSend 性質適用於除 Message。 例如,如果你用成員指定標籤或優先權 DefaultPropertiesToSend ,這些值會適用於任何包含物件且在應用程式送入隊列時非型別 Message 物件的訊息。 當傳送 時 Message,設定的 Message 屬性值優先於 DefaultPropertiesToSend,訊息的 Message.Formatter 屬性優先於佇列 MessageQueue.Formatter 屬性。
下表顯示此方法是否可在多種工作群組模式中使用。
| 工作群組模式 | 有現貨 |
|---|---|
| 本機電腦 | 是的 |
| 本地電腦與直接格式名稱 | 是的 |
| 遠端電腦 | No |
| 遠端電腦與直接格式名稱 | 是的 |
另請參閱
- MessageQueueTransactionType
- DefaultPropertiesToSend
- Message
- Transactional
- Label
- Peek()
- Receive()
- BeginPeek()
- BeginReceive()