語言

MessageQueue.ReceiveById 方法

定義

接收與該識別碼相符的訊息,並將其從佇列中移除。

多載

名稱 Description
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 。

傳回

其MessageId屬性與輸入的參數相符id。

例外狀況

參數 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) 方法總是回傳佇列中的第一個訊息,因此後續呼叫該方法也會回傳相同的訊息,除非佇列中有更高優先權的訊息。

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

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

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 。

傳回

其MessageId屬性與輸入的參數相符id。

例外狀況

參數 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) 不會移除佇列中的任何訊息,如果交易被中止,就沒有什麼可以回滾的。

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

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

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 值,描述要與訊息關聯的交易上下文類型。

傳回

其MessageId屬性與輸入的參數相符id。

例外狀況

參數 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 的交易上下文返回給應用程式。

請指定AutomatictransactionType參數,是否已經連接到你想用來接收訊息的執行緒。 請指定 Single 你是否想將訊息作為單一內部交易接收。 你可以指定 None 是否想在交易上下文外接收來自交易隊列的訊息。

Id訊息的屬性在整個訊息佇列企業中是唯一的,因此隊列中最多只有一則符合該id參數的訊息。 若該訊息與該實例關聯的隊 MessageQueue 列不同,則無法找到該訊息。

若呼叫此方法以接收交易佇列中的訊息,交易中止時,收到的訊息會被回傳至佇列。 在交易提交之前,訊息不會永久從佇列中移除。

另外兩種方法可以讓你從佇列中接收訊息。 該 Receive 方法會回傳佇列中的第一個訊息,並用來 ReceiveByCorrelationId(String) 擷取因發送到佇列訊息而產生的確認、報告或應用程式產生的回應訊息。

若要讀取指定識別碼的訊息而不將其從佇列中移除,請使用該 PeekById(String) 方法。 該 PeekById(String) 方法總是回傳佇列中的第一個訊息,因此後續呼叫該方法也會回傳相同的訊息,除非佇列中有更高優先權的訊息。 呼叫回 PeekById(String)傳的訊息沒有交易上下文。 由於 PeekById(String) 不會移除佇列中的任何訊息,如果交易被中止,就沒有什麼可以回滾的。

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

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

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 A表示等待新訊息可檢查的時間。

傳回

其MessageId屬性與輸入的參數相符id。

例外狀況

參數 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參數的訊息。

利用這個超載,當 ReceiveById(String) 當前執行緒被阻塞時,只要新訊息持續在參數指定的 timeout 逾時期內抵達佇列,就可接受。 執行緒將被封鎖至少在指定時間內,或若你指定參數值InfiniteTimeouttimeout,或在參數指定的timeout逾時期內仍有新訊息進入佇列,則會無限期封鎖。

另外兩種方法可以讓你從佇列中接收訊息。 該 Receive 方法會回傳佇列中的第一個訊息,並用來 ReceiveByCorrelationId(String) 擷取因發送到佇列訊息而產生的確認、報告或應用程式產生的回應訊息。

若要讀取指定識別碼的訊息而不將其從佇列中移除,請使用該 PeekById(String) 方法。 該 PeekById(String) 方法總是回傳佇列中的第一個訊息,因此後續呼叫該方法也會回傳相同的訊息,除非佇列中有更高優先權的訊息。

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

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

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 A表示等待新訊息可檢查的時間。

傳回

其MessageId屬性與輸入的參數相符id。

例外狀況

參數 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參數的訊息。

利用這個超載,當 ReceiveById(String) 當前執行緒被阻塞時,只要新訊息持續在參數指定的 timeout 逾時期內抵達佇列,就可接受。 執行緒將被封鎖至少在指定時間內,若你指定參數值InfiniteTimeouttimeout,或在參數指定的timeout逾時期內仍有新訊息抵達佇列,則會無限期封鎖。

由於此方法是在交易佇列中呼叫,若交易中止,收到的訊息會被回傳至佇列。 在交易提交之前,訊息不會永久從佇列中移除。

另外兩種方法可以讓你從佇列中接收訊息。 該 Receive 方法會回傳佇列中的第一個訊息,並用來 ReceiveByCorrelationId(String) 擷取因發送到佇列訊息而產生的確認、報告或應用程式產生的回應訊息。

若要讀取指定識別碼的訊息而不將其從佇列中移除,請使用該 PeekById(String) 方法。 該 PeekById(String) 方法總是回傳佇列中的第一個訊息,因此後續呼叫該方法也會回傳相同的訊息,除非佇列中有更高優先權的訊息抵達。 呼叫回 PeekById(String)傳的訊息沒有交易上下文。 由於 PeekById(String) 不會移除佇列中的任何訊息,如果交易被中止,就沒有什麼可以回滾的。

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

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

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 A表示等待新訊息可檢查的時間。

transactionType
MessageQueueTransactionType

其中一個 MessageQueueTransactionType 值,描述要與訊息關聯的交易上下文類型。

傳回

其MessageId屬性與輸入的參數相符id。

例外狀況

參數 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);

備註

使用此方法讀取已知識別碼的訊息並將其從隊列中移除。 如果該訊息的識別碼 id 在佇列中,且使用由 transactionType 參數定義的交易上下文,此方法會立即回傳。 否則,方法會等待指定時間等待新訊息抵達。 若逾時期內未收到新訊息,則會拋出例外。

該 timeout 參數未指定此方法的總執行時間。 它指定等待新訊息到達隊列的時間。 每當有新訊息抵達時,此方法會檢查該訊息的參數Id是否符合。id 若未完成,則重新開始逾時,等待另一則新訊息抵達。 因此,如果在逾時期內持續有新訊息抵達,這個方法有可能無限期地持續運行,無論是直到逾時期結束且沒有新訊息到達,或直到收到符合 Id 參數的 id 訊息。

請指定AutomatictransactionType參數,是否已經連接到你想用來接收訊息的執行緒。 請指定 Single 你是否想將訊息作為單一內部交易接收。 你可以指定 None 是否想在交易上下文外接收來自交易隊列的訊息。

Id訊息的屬性在整個訊息佇列企業中是唯一的,因此隊列中最多只有一則符合該id參數的訊息。 若該訊息與該實例關聯的隊 MessageQueue 列不同,則無法找到該訊息。

利用這個超載,當 ReceiveById(String) 當前執行緒被阻塞時,只要新訊息持續在參數指定的 timeout 逾時期內抵達佇列,就可接受。 執行緒將被封鎖至少在指定時間內,或若你指定參數值InfiniteTimeouttimeout,或在參數指定的timeout逾時期內仍有新訊息進入佇列,則會無限期封鎖。

若呼叫此方法以接收交易佇列中的訊息,交易中止時,收到的訊息會被回傳至佇列。 在交易提交之前,訊息不會永久從佇列中移除。

另外兩種方法可以讓你從佇列中接收訊息。 該 Receive 方法會回傳佇列中的第一個訊息,並用來 ReceiveByCorrelationId(String) 擷取因發送到佇列訊息而產生的確認、報告或應用程式產生的回應訊息。

若要讀取指定識別碼的訊息而不將其從佇列中移除,請使用該 PeekById(String) 方法。 該 PeekById(String) 方法總是回傳佇列中的第一個訊息,因此後續呼叫該方法也會回傳相同的訊息,除非佇列中有更高優先權的訊息。 呼叫回 PeekById(String)傳的訊息沒有交易上下文。 由於 PeekById(String) 不會移除佇列中的任何訊息,如果交易被中止,就沒有什麼可以回滾的。

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

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於