我目前正在尝试从会话中检索特定邮件。
为此,我想在 MessageSession 上使用。接收( Int64 ),其中我传递序列号码。
这是我的代码 -
long msgSequenceNr = 1337;
QueueClient queueClient = QueueClient.CreateFromConnectionString(Constants.ServiceBusConnectionString, Constants.TestQueueEntityName, ReceiveMode.PeekLock);
MessageSession msgSession = queueClient.AcceptMessageSession(Constants.TestSessionId);
var peekedMsg = msgSession.Peek(msgSequenceNr); // <-- Works fine!
var receivedMsg = msgSession.Receive(msgSequenceNr); // <-- MessageNotFoundException
不幸的是,当Peek工作正常时,Receive将导致 MessageNotFoundException 。 这是我错过的限制还是有另一种方法来实现这一目标。
请注意,会话中可能有多条消息
答案 0 :(得分:3)
使用SequenceNumber接收只能与Defer方法结合使用。这就是你实现它的方式:
示例应用程序:https://code.msdn.microsoft.com/windowsazure/Brokered-Messaging-ccc4f879#content
<强>更新强>
形成你的评论我注意到&#34;未发送&#34;延期消息可能是一种解决方案。这是一些示例代码,用于取消将延迟消息复制到新消息的消息,完成延迟消息并将新消息发送回队列中。这使用TransactionScope以事务方式完成并重新发送消息,以避免丢失消息的风险:
var messageId = "12434539828282";
// Send.
var msg = new BrokeredMessage {SessionId = "user1", MessageId = messageId };
msg.Properties.Add("Language", "Dutch");
queue.Send(msg);
// Receive.
var session = queue.AcceptMessageSession();
msg = session.Receive();
// Store the sequence number.
var sequenceNumber = msg.SequenceNumber;
// Defer.
msg.Defer();
// Change to true to test if the transaction worked.
var shouldThrow = false;
// Later processing of deferred message.
msg = session.Receive(sequenceNumber);
try
{
using (var ts = new TransactionScope())
{
// Create a new message.
var undeferredMessage = new BrokeredMessage {SessionId = msg.SessionId, MessageId = msg.MessageId};
foreach (var prop in msg.Properties)
undeferredMessage.Properties.Add(prop);
// Complete and send within the same transaction.
msg.Complete();
if (shouldThrow)
throw new InvalidOperationException("Some error");
queue.Send(undeferredMessage);
// Complete the transaction.
ts.Complete();
}
}
catch (Exception ex)
{
msg.Abandon();
}
if (shouldThrow)
{
msg = session.Receive(sequenceNumber);
Console.WriteLine(msg.MessageId + " should match: " + messageId);
}
else
{
try
{
msg = session.Receive(sequenceNumber);
}
catch (Exception ex)
{
Console.WriteLine("Message not found, transaction worked OK.");
}
}
注意:这里我只是简单地获取了属性的副本。请注意您可能要复制正文和任何其他附加信息。