下面代码的正常预期行为是ReceiveAsync,在返回null之前查看Azure队列最多1分钟,或者如果收到消息则查看消息。对此的预期用途是具有IoT中枢资源,其中可以将多个消息添加到旨在用于若干DeviceClient对象之一的队列中。每个DeviceClient将连续轮询此队列以接收针对它的消息。因此,其他DeviceClient的消息将留在队列中以供其他人使用。
实际行为是ReceiveAsync每次调用时都会立即返回null,没有延迟。这与TimeSpan给出的值无关 - 或者如果没有给出参数(并且使用默认时间)。
所以,不是每分钟看到1个日志项,而是说收到了一条空消息,我每秒得到2个日志项(!)。这种行为与几个月前不同。所以我开始做一些研究 - 到目前为止收效甚微。
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Client;
public static TimeSpan receiveMessageWaitTime = new TimeSpan(0, 1 , 0);
Microsoft.Azure.Devices.Client.Message receivedMessage = null;
deviceClient = DeviceClient.CreateFromConnectionString(Settings.lastKnownConnectionString, Microsoft.Azure.Devices.Client.TransportType.Amqp);
// This code is within an infinite loop/task/with try/except code
if(deviceClient != null)
{
receivedMessage = await deviceClient.ReceiveAsync(receiveMessageWaitTime);
if(receivedMessage != null)
{
string Json = Encoding.ASCII.GetString(receivedMessage.GetBytes());
// Handle the message
}
else
{
// Log the fact that we got a null message, and try again later
}
await Task.Delay(500); // Give the CPU some time, this is an infinite loop after all.
}
我查看了Azure中心,发现队列中有8条消息。然后我再添加了2个,并且没有收到任何新消息,现在队列中有10个项目。
我注意到了这个问题:Azure ServiceBus: Client.Receive() returns null for messages > 64 KB 但我无法确定队列中是否确实存在大的消息(因为receivemessage返回null ...)
就这样问题:
非常感谢任何帮助。
答案 0 :(得分:0)
如果您使用Azure ServiceBus,我建议您使用Service Bus Explorer预览邮件,获取队列中的邮件数量。此外,您可以删除邮件而无需获取邮件。