当收到多条消息时,我在关闭MessageSession时遇到问题...我等待所有序列进入然后我尝试"完成()&#34 ;会话中的所有消息。我已经尝试了很多不同的方法来迭代收到的消息和"完成()" - 他们,"关闭()" - 会话,"完成()" - 会议,我找不到我生命中的胜利公式。微软的文档也不是很有帮助。它当然没有提供一个体面的例子。以下是我在其中最新迭代的代码
public QueueMessageBody ReceiveResponse(string sessionId) {
try
{
MessagingFactory _messagingFactory;
_messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);
var receiver = _messagingFactory.CreateQueueClient(ResponseQueue, ReceiveMode.PeekLock);
var messageBodyStream = new MemoryStream();
while (true)
{
Console.WriteLine("Waiting for new message");
using (var scope = new TransactionScope())
{
var receiverSession = receiver.AcceptMessageSession(sessionId);
messageBodyStream = new MemoryStream();
var keepPolling = true;
var isFirstMessage = true;
var expectedNoMessages = 0;
BrokeredMessage[] messages = null;
var messagesReceived = 0;
while (keepPolling)
{
var message = receiverSession.Receive(TimeSpan.FromSeconds(10));
if (message == null)
continue;
if (isFirstMessage)
{
Console.WriteLine("Receiving first message");
expectedNoMessages = (int)message.Properties["TotalMessages"];
messages = new BrokeredMessage[expectedNoMessages];
isFirstMessage = false;
}
var messageNo = (int)message.Properties["MessageNo"];
var messageIndex = messageNo - 1;
Console.WriteLine(string.Format("Receiving message {0}", messageNo));
messages[messageIndex] = message;
messagesReceived++;
if (messagesReceived == expectedNoMessages)
keepPolling = false;
}
//Rebuild Object
var messageBodyBuilder = new ChunkedMessageBuilder().ReconstructMessageBody(messages);
var o = SerializationHelper.Deserialize<QueueMessageBody>(messageBodyBuilder);
var completeTasks = new List<Task>();
if (messages.Count()< 3)
{
foreach (var message in messages)
completeTasks.Add(receiverSession.CompleteAsync(message.LockToken));
Task.WaitAll(completeTasks.ToArray());
}else
{
receiverSession.Close();
}
return o;
}
}
}
catch(Exception ex)
{
System.Diagnostics.Debug.Print(ex.Message);
return null;
}
}
public QueueMessageBody ReceiveResponse(string sessionId)
{
try
{
MessagingFactory _messagingFactory;
_messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);
var receiver = _messagingFactory.CreateQueueClient(ResponseQueue, ReceiveMode.PeekLock);
var messageBodyStream = new MemoryStream();
while (true)
{
Console.WriteLine("Waiting for new message");
using (var scope = new TransactionScope())
{
var receiverSession = receiver.AcceptMessageSession(sessionId);
messageBodyStream = new MemoryStream();
var keepPolling = true;
var isFirstMessage = true;
var expectedNoMessages = 0;
BrokeredMessage[] messages = null;
var messagesReceived = 0;
while (keepPolling)
{
var message = receiverSession.Receive(TimeSpan.FromSeconds(10));
if (message == null)
continue;
if (isFirstMessage)
{
Console.WriteLine("Receiving first message");
expectedNoMessages = (int)message.Properties["TotalMessages"];
messages = new BrokeredMessage[expectedNoMessages];
isFirstMessage = false;
}
var messageNo = (int)message.Properties["MessageNo"];
var messageIndex = messageNo - 1;
Console.WriteLine(string.Format("Receiving message {0}", messageNo));
messages[messageIndex] = message;
messagesReceived++;
if (messagesReceived == expectedNoMessages)
keepPolling = false;
}
//Rebuild Object
var messageBodyBuilder = new ChunkedMessageBuilder().ReconstructMessageBody(messages);
var o = SerializationHelper.Deserialize<QueueMessageBody>(messageBodyBuilder);
var completeTasks = new List<Task>();
if (messages.Count()< 3)
{
foreach (var message in messages)
completeTasks.Add(receiverSession.CompleteAsync(message.LockToken));
Task.WaitAll(completeTasks.ToArray());
}else
{
receiverSession.Close();
}
return o;
}
}
}
catch(Exception ex)
{
System.Diagnostics.Debug.Print(ex.Message);
return null;
}
}
队列配置如下。
我能够接收消息并完成工作流程,问题是他们只是留在队列中。我无法在
完成它们答案 0 :(得分:0)
在这种情况下,问题可能是事务从未完成。删除TransactionScope摆脱了这个问题。