MessageQueue.ReceiveById 方法

定義

接收符合指定識別項的訊息,並將它從佇列中移除。

多載

ReceiveById(String)

從非交易佇列中接收符合指定識別項的訊息,如果佇列中目前不存在具有指定識別項的訊息,則立即引發例外狀況。

ReceiveById(String, MessageQueueTransaction)

接收符合指定識別項的訊息 (從交易佇列中),如果佇列中目前不存在具有指定識別項的訊息,則立即引發例外狀況。

ReceiveById(String, MessageQueueTransactionType)

接收符合指定識別項的訊息,如果佇列中目前不存在具有指定識別項的訊息,則立即引發例外狀況。

ReceiveById(String, TimeSpan)

接收符合指定識別項的訊息 (從非交易佇列中),並且等待佇列中出現具有指定識別項的訊息,或者逾時到期。

ReceiveById(String, TimeSpan, MessageQueueTransaction)

接收符合指定識別項的訊息 (從交易佇列中),並且等待佇列中出現具有指定識別項的訊息,或者逾時到期。

ReceiveById(String, TimeSpan, MessageQueueTransactionType)

接收符合指定識別項的訊息,並且等待佇列中出現具有指定識別項的訊息,或者逾時到期。

ReceiveById(String)

從非交易佇列中接收符合指定識別項的訊息,如果佇列中目前不存在具有指定識別項的訊息,則立即引發例外狀況。

public:
 System::Messaging::Message ^ ReceiveById(System::String ^ id);
public System.Messaging.Message ReceiveById (string id);
member this.ReceiveById : string -> System.Messaging.Message
Public Function ReceiveById (id As String) As Message

參數

id
String

要接收訊息的 Id

傳回

傳入符合 id 參數的 MessageId

例外狀況

id 參數為 null

找不到具有指定 id 的訊息。

存取訊息佇列方法時發生錯誤。

範例

下列程式碼範例示範 ReceiveById(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");

// Get the message's Id property value.
String^ id = msg->Id;

// Simulate doing other work so the message has time to arrive.
System::Threading::Thread::Sleep(TimeSpan::FromSeconds(10.0));

// Receive the message from the queue.
msg = queue->ReceiveById(id);

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");

// Get the message's Id property value.
string id = msg.Id;

// Simulate doing other work so the message has time to arrive.
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10.0));

// Receive the message from the queue.
msg = queue.ReceiveById(id);

備註

使用此方法可讀取具有已知標識符的訊息,並將其從佇列中移除。 如果訊息不在佇列中,這個方法會立即擲回例外狀況。

Id訊息的 屬性在整個消息佇列企業中是唯一的,因此佇列中最多會有一個符合指定id參數的訊息。

其他兩種方法可讓您從佇列接收訊息。 方法 Receive 會傳回佇列中的第一個訊息,而 ReceiveByCorrelationId(String) 方法會用來擷取通知、報告或應用程式產生的回應消息,因為訊息傳送至佇列而建立。

若要讀取具有指定標識符的訊息,而不從佇列中移除它,請使用 PeekById(String) 方法。 方法 PeekById(String) 一律會傳回佇列中的第一個訊息,因此除非佇列中有較高的優先順序訊息,否則對方法的後續呼叫會傳回相同的訊息。

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

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

另請參閱

適用於

ReceiveById(String, MessageQueueTransaction)

接收符合指定識別項的訊息 (從交易佇列中),如果佇列中目前不存在具有指定識別項的訊息,則立即引發例外狀況。

public:
 System::Messaging::Message ^ ReceiveById(System::String ^ id, System::Messaging::MessageQueueTransaction ^ transaction);
public System.Messaging.Message ReceiveById (string id, System.Messaging.MessageQueueTransaction transaction);
member this.ReceiveById : string * System.Messaging.MessageQueueTransaction -> System.Messaging.Message
Public Function ReceiveById (id As String, transaction As MessageQueueTransaction) As Message

參數

id
String

要接收訊息的 Id

傳回

傳入符合 id 參數的 MessageId

例外狀況

id 參數為 null

-或-

transaction 參數為 null

找不到具有指定 id 的訊息。

該佇列是非交易式佇列。

-或-

存取訊息佇列方法時發生錯誤。

範例

下列程式碼範例示範 ReceiveById(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");

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

// Get the message's Id property value.
String^ id = msg->Id;

// Simulate doing other work so the message has time to arrive.
System::Threading::Thread::Sleep(TimeSpan::FromSeconds(10.0));

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

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

    // Receive the message from the queue.
    msg = queue->ReceiveById(id, 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");

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

// Get the message's Id property value.
string id = msg.Id;

// Simulate doing other work so the message has time to arrive.
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10.0));

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

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

    // Receive the message from the queue.
    msg = queue.ReceiveById(id, 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 定義的內部交易內容,從佇列中移除該訊息。 如果訊息不在佇列中,這個方法會立即擲回例外狀況

Id訊息的 屬性在整個消息佇列企業中是唯一的,因此佇列中最多會有一個符合指定id參數的訊息。

因為這個方法是在交易佇列上呼叫,所以如果交易已中止,就會將收到的訊息傳回至佇列。 在認可交易之前,不會從佇列永久移除訊息。

其他兩種方法可讓您從佇列接收訊息。 方法 Receive 會傳回佇列中的第一個訊息,而 ReceiveByCorrelationId(String) 方法會用來擷取通知、報告或應用程式產生的回應消息,因為訊息傳送至佇列而建立。

若要讀取具有指定標識符的訊息,而不從佇列中移除它,請使用 PeekById(String) 方法。 方法 PeekById(String) 一律會傳回佇列中的第一個訊息,因此除非佇列中有較高的優先順序訊息,否則對方法的後續呼叫會傳回相同的訊息。 呼叫 所 PeekById(String)傳回的訊息沒有相關聯的交易內容。 由於 PeekById(String) 不會移除佇列中的任何訊息,因此如果交易中止,則不會復原任何訊息。

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

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

另請參閱

適用於

ReceiveById(String, MessageQueueTransactionType)

接收符合指定識別項的訊息,如果佇列中目前不存在具有指定識別項的訊息,則立即引發例外狀況。

public:
 System::Messaging::Message ^ ReceiveById(System::String ^ id, System::Messaging::MessageQueueTransactionType transactionType);
public System.Messaging.Message ReceiveById (string id, System.Messaging.MessageQueueTransactionType transactionType);
member this.ReceiveById : string * System.Messaging.MessageQueueTransactionType -> System.Messaging.Message
Public Function ReceiveById (id As String, transactionType As MessageQueueTransactionType) As Message

參數

id
String

要接收訊息的 Id

transactionType
MessageQueueTransactionType

其中一個 MessageQueueTransactionType 值,描述要與訊息相關聯的異動內容的類型。

傳回

傳入符合 id 參數的 MessageId

例外狀況

id 參數為 null

找不到具有指定 id 的訊息。

transactionType 參數不是其中一個 MessageQueueTransactionType 成員。

存取訊息佇列方法時發生錯誤。

範例

下列程式碼範例示範 ReceiveById(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);

// Get the message's Id property value.
String^ id = msg->Id;

// Simulate doing other work so the message has time to arrive.
System::Threading::Thread::Sleep(TimeSpan::FromSeconds(10.0));

// Receive the message from the queue.
msg = queue->ReceiveById(id, 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);

// Get the message's Id property value.
string id = msg.Id;

// Simulate doing other work so the message has time to arrive.
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10.0));

// Receive the message from the queue.
msg = queue.ReceiveById(id, MessageQueueTransactionType.Single);

備註

使用此方法可讀取具有已知標識符的訊息,並將其從佇列中移除。 如果訊息不在佇列中,這個方法會立即擲回例外狀況。 否則,訊息會從佇列中移除,並使用 參數所 transactionType 定義的交易內容傳回應用程式。

Automatic如果已經附加至您要用來接收訊息之線程的外部交易內容,請指定 transactionType 參數。 指定 Single 是否要以單一內部交易的形式接收訊息。 您可以指定 None 是否要從交易內容外部的交易佇列接收訊息。

Id訊息的 屬性在整個消息佇列企業中是唯一的,因此佇列中最多會有一個符合指定id參數的訊息。 如果具有指定標識碼的訊息位於與此 MessageQueue 實例相關聯的訊息以外的佇列中,則找不到訊息。

如果呼叫這個方法以接收來自交易佇列的訊息,如果交易已中止,則收到的訊息會傳回至佇列。 在認可交易之前,不會從佇列永久移除訊息。

其他兩種方法可讓您從佇列接收訊息。 方法 Receive 會傳回佇列中的第一個訊息,而 ReceiveByCorrelationId(String) 方法會用來擷取通知、報告或應用程式產生的回應消息,因為訊息傳送至佇列而建立。

若要讀取具有指定標識符的訊息,而不從佇列中移除它,請使用 PeekById(String) 方法。 方法 PeekById(String) 一律會傳回佇列中的第一個訊息,因此除非佇列中有較高的優先順序訊息,否則對方法的後續呼叫會傳回相同的訊息。 呼叫 所 PeekById(String)傳回的訊息沒有相關聯的交易內容。 由於 PeekById(String) 不會移除佇列中的任何訊息,因此如果交易中止,則不會復原任何訊息。

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

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

另請參閱

適用於

ReceiveById(String, TimeSpan)

接收符合指定識別項的訊息 (從非交易佇列中),並且等待佇列中出現具有指定識別項的訊息,或者逾時到期。

public:
 System::Messaging::Message ^ ReceiveById(System::String ^ id, TimeSpan timeout);
public System.Messaging.Message ReceiveById (string id, TimeSpan timeout);
member this.ReceiveById : string * TimeSpan -> System.Messaging.Message
Public Function ReceiveById (id As String, timeout As TimeSpan) As Message

參數

id
String

要接收訊息的 Id

timeout
TimeSpan

TimeSpan,指出等待新訊息可以進行檢查的時間。

傳回

傳入符合 id 參數的 MessageId

例外狀況

id 參數為 null

timeout 參數指定的值無效,可能是 timeout 小於 Zero 或大於 InfiniteTimeout

具有指定 id 的訊息未在逾時到期前到達佇列中。

-或-

存取訊息佇列方法時發生錯誤。

範例

下列程式碼範例示範 ReceiveById(String, TimeSpan) 的用法。


// 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");

// Get the message's Id property value.
String^ id = msg->Id;

// Receive the message from the queue.
msg = queue->ReceiveById(id, TimeSpan::FromSeconds(10.0));

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");

// Get the message's Id property value.
string id = msg.Id;

// Receive the message from the queue.
msg = queue.ReceiveById(id, TimeSpan.FromSeconds(10.0));

備註

使用此方法可讀取具有已知標識符的訊息,並將其從佇列中移除。 如果參數指定的 id 訊息位於佇列中,這個方法會立即傳回。 否則,方法會等候指定的時間週期,讓新訊息送達。 如果在逾時到期之前未送達新訊息,則會擲回例外狀況。

參數 timeout 未指定這個方法的總運行時間。 相反地,它會指定等候新訊息抵達佇列的時間。 每次新訊息送達時,這個方法都會 Id 檢查新訊息的 ,以查看它 id 是否符合 參數。 如果沒有,這個方法會開始逾時期間,並等候另一個新訊息送達。 因此,如果新訊息繼續在逾時期間內送達,則此方法可以無限期地繼續執行,直到逾時期間到期,而不會到達任何新訊息,或直到訊息到達符合 Id 參數的 id 訊息為止。

Id訊息的 屬性在整個消息佇列企業中是唯一的,因此佇列中最多會有一個符合指定id參數的訊息。

當目前的線程可接受此多載時,只要新訊息繼續在 參數所指定的逾時期間內繼續抵達佇列,就會timeout使用此多載ReceiveById(String)。 線程會至少封鎖指定的一段時間,或無限期地指定 參數的值InfiniteTimeouttimeout,或新訊息繼續在參數指定的timeout逾時期間內抵達佇列。

其他兩種方法可讓您從佇列接收訊息。 方法 Receive 會傳回佇列中的第一個訊息,而 ReceiveByCorrelationId(String) 方法會用來擷取通知、報告或應用程式產生的回應消息,因為訊息傳送至佇列而建立。

若要讀取具有指定標識符的訊息,而不從佇列中移除它,請使用 PeekById(String) 方法。 方法 PeekById(String) 一律會傳回佇列中的第一個訊息,因此除非佇列中有較高的優先順序訊息,否則對方法的後續呼叫會傳回相同的訊息。

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

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

另請參閱

適用於

ReceiveById(String, TimeSpan, MessageQueueTransaction)

接收符合指定識別項的訊息 (從交易佇列中),並且等待佇列中出現具有指定識別項的訊息,或者逾時到期。

public:
 System::Messaging::Message ^ ReceiveById(System::String ^ id, TimeSpan timeout, System::Messaging::MessageQueueTransaction ^ transaction);
public System.Messaging.Message ReceiveById (string id, TimeSpan timeout, System.Messaging.MessageQueueTransaction transaction);
member this.ReceiveById : string * TimeSpan * System.Messaging.MessageQueueTransaction -> System.Messaging.Message
Public Function ReceiveById (id As String, timeout As TimeSpan, transaction As MessageQueueTransaction) As Message

參數

id
String

要接收訊息的 Id

timeout
TimeSpan

TimeSpan,指出等待新訊息可以進行檢查的時間。

傳回

傳入符合 id 參數的 MessageId

例外狀況

id 參數為 null

-或-

transaction 參數為 null

timeout 參數指定的值無效,可能是 timeout 小於 Zero 或大於 InfiniteTimeout

具有指定 id 的訊息未在逾時到期前到達佇列中。

-或-

該佇列是非交易式佇列。

-或-

存取訊息佇列方法時發生錯誤。

範例

下列程式碼範例示範 ReceiveById(String, TimeSpan, 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");

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

// Get the message's Id property value.
String^ id = msg->Id;

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

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

    // Receive the message from the queue.
    msg = queue->ReceiveById(id, TimeSpan::FromSeconds(10.0),
        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");

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

// Get the message's Id property value.
string id = msg.Id;

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

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

    // Receive the message from the queue.
    msg = queue.ReceiveById(id, TimeSpan.FromSeconds(10.0),
        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 定義的內部交易內容,從佇列中移除該訊息。 如果參數指定的 id 訊息位於佇列中,這個方法會立即傳回。 否則,方法會等候指定的時間週期,讓新訊息送達。 如果在逾時到期之前未送達新訊息,則會擲回例外狀況。

參數 timeout 未指定這個方法的總運行時間。 相反地,它會指定等候新訊息抵達佇列的時間。 每次新訊息送達時,這個方法都會 Id 檢查新訊息的 ,以查看它 id 是否符合 參數。 如果沒有,這個方法會開始逾時期間,並等候另一個新訊息送達。 因此,如果新訊息繼續在逾時期間內送達,則此方法可以無限期地繼續執行,直到逾時期間到期,而不會到達任何新訊息,或直到訊息到達符合 Id 參數的 id 訊息為止。

Id訊息的 屬性在整個消息佇列企業中是唯一的,因此佇列中最多會有一個符合指定id參數的訊息。

當目前的線程可接受此多載時,只要新訊息繼續在 參數所指定的逾時期間內繼續抵達佇列,就會timeout使用此多載ReceiveById(String)。 線程至少會封鎖指定的一段時間,或無限期地指定 參數的值InfiniteTimeouttimeout,或如果新訊息繼續在參數指定的timeout逾時期間內到達佇列,

因為這個方法是在交易佇列上呼叫,所以如果交易已中止,就會將收到的訊息傳回至佇列。 在認可交易之前,不會從佇列永久移除訊息。

其他兩種方法可讓您從佇列接收訊息。 方法 Receive 會傳回佇列中的第一個訊息,而 ReceiveByCorrelationId(String) 方法會用來擷取通知、報告或應用程式產生的回應消息,因為訊息傳送至佇列而建立。

若要讀取具有指定標識符的訊息,而不從佇列中移除它,請使用 PeekById(String) 方法。 方法 PeekById(String) 一律會傳回佇列中的第一個訊息,因此除非佇列中有較高的優先順序訊息,否則對方法的後續呼叫會傳回相同的訊息。 呼叫 所 PeekById(String)傳回的訊息沒有相關聯的交易內容。 由於 PeekById(String) 不會移除佇列中的任何訊息,因此如果交易中止,則不會復原任何訊息。

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

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

另請參閱

適用於

ReceiveById(String, TimeSpan, MessageQueueTransactionType)

接收符合指定識別項的訊息,並且等待佇列中出現具有指定識別項的訊息,或者逾時到期。

public:
 System::Messaging::Message ^ ReceiveById(System::String ^ id, TimeSpan timeout, System::Messaging::MessageQueueTransactionType transactionType);
public System.Messaging.Message ReceiveById (string id, TimeSpan timeout, System.Messaging.MessageQueueTransactionType transactionType);
member this.ReceiveById : string * TimeSpan * System.Messaging.MessageQueueTransactionType -> System.Messaging.Message
Public Function ReceiveById (id As String, timeout As TimeSpan, transactionType As MessageQueueTransactionType) As Message

參數

id
String

要接收訊息的 Id

timeout
TimeSpan

TimeSpan,指出等待新訊息可以進行檢查的時間。

transactionType
MessageQueueTransactionType

其中一個 MessageQueueTransactionType 值,描述要與訊息相關聯的異動內容的類型。

傳回

傳入符合 id 參數的 MessageId

例外狀況

id 參數為 null

timeout 參數指定的值無效,可能是 timeout 小於 Zero 或大於 InfiniteTimeout

具有指定 id 的訊息未在逾時到期前到達佇列中。

-或-

存取訊息佇列方法時發生錯誤。

transactionType 參數不是其中一個 MessageQueueTransactionType 成員。

範例

下列程式碼範例示範 ReceiveById(String, TimeSpan, 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);

// Get the message's Id property value.
String^ id = msg->Id;

// Receive the message from the queue.
msg = queue->ReceiveById(id, TimeSpan::FromSeconds(10.0),
    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);

// Get the message's Id property value.
string id = msg.Id;

// Receive the message from the queue.
msg = queue.ReceiveById(id, TimeSpan.FromSeconds(10.0),
    MessageQueueTransactionType.Single);

備註

使用這個方法來讀取具有已知標識符的訊息,並將它從佇列中移除。 如果使用 參數所transactionType定義的交易內容,具有 參數所id指定標識符的訊息在佇列中,這個方法會立即傳回。 否則,方法會等候指定的時間週期,讓新的訊息送達。 如果在逾時到期之前未送達新訊息,則會擲回例外狀況。

參數 timeout 不會指定這個方法的總運行時間。 相反地,它會指定等候新訊息抵達佇列的時間。 每次新訊息送達時,這個方法會 Id 檢查新訊息的 ,以查看它 id 是否符合 參數。 如果沒有,這個方法會啟動逾時期間,並等候另一個新的訊息送達。 因此,如果新訊息繼續在逾時期間內送達,則這個方法可能會無限期地繼續執行,直到逾時期間到期,而不會到達任何新的訊息,或直到符合 參數的id訊息送達Id為止。

Automatic如果已經附加至您想要用來接收訊息之線程的外部交易內容,請指定 transactionType 參數。 指定 Single 是否要以單一內部交易的形式接收訊息。 您可以指定 None 是否要從交易內容外部的交易佇列接收訊息。

Id訊息的屬性在消息佇列企業中是唯一的,因此佇列中最多會有一個符合指定id參數的訊息。 如果具有指定標識碼的訊息位於與此實例相關聯 MessageQueue 之訊息以外的佇列中,則找不到訊息。

只要新訊息繼續在 參數指定的timeout逾時期間內繼續抵達佇列,就可以在 目前的線程可接受時使用此 多載ReceiveById(String)。 線程至少會封鎖指定的一段時間,或者如果您指定 參數的值InfiniteTimeouttimeout,或如果新訊息繼續在 參數指定的timeout逾時期間內繼續抵達佇列,則為無限期。

如果呼叫這個方法以接收來自交易佇列的訊息,則會在中止交易時傳回至佇列的訊息。 在認可交易之前,訊息不會永久從佇列中移除。

另外兩種方法可讓您接收來自佇列的訊息。 方法 Receive 會傳回佇列中的第一個訊息,而 ReceiveByCorrelationId(String) 方法會用來擷取通知、報告或應用程式產生的回應消息,因為訊息傳送至佇列而建立。

若要讀取具有指定標識符的訊息,而不從佇列中移除它,請使用 PeekById(String) 方法。 方法 PeekById(String) 一律會傳回佇列中的第一個訊息,因此後續呼叫 方法會傳回相同的訊息,除非優先順序較高的訊息抵達佇列。 呼叫 所 PeekById(String)傳回的訊息沒有相關聯的交易內容。 由於 PeekById(String) 不會移除佇列中的任何訊息,因此如果交易中止,則不會復原任何訊息。

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

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

另請參閱

適用於