【发布时间】:2018-03-14 18:54:02
【问题描述】:
以下代码的正常预期行为是 ReceiveAsync 会在 Azure 队列中最多查看 1 分钟,然后返回 null 或如果收到则返回一条消息。这样做的预期用途是拥有 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...)
这样的问题:
- 能否预览队列中的消息?
- 您能否获取队列大小,例如在获取消息之前询问队列中的消息数量?
- 能否在不获取消息的情况下从队列中删除消息?
- 能否创建基于回调的接收而不是无限循环? (我猜在内部,代码只会偷看一下,就像我们已经在做的那样)
任何帮助将不胜感激。
【问题讨论】: