我正在编写一些代码来监视我们的 azure 服务总线队列和主题,我想通知我们是否有旧消息。我的代码可以工作,但如果订阅中没有任何事件消息,那么它将挂起一分钟,然后抛出一个异常:
Unhandled exception. Azure.Messaging.ServiceBus.ServiceBusException: The operation did not complete within the allocated time 00:01:00 for object receiver12. (ServiceTimeout)
---> System.TimeoutException: The operation did not complete within the allocated time 00:01:00 for object receiver12.
看起来它只发生在启用 session 的订阅上。我尝试首先查看订阅中是否有任何消息,如果有,则查看它们,但通常会在检查消息计数和查看之间收到消息,因此会发生异常。我的监控解决方案无法处理那么长的超时。如果订阅为空,我只想检索并清空列表。 也尝试过 python 实现,它是相同的,所以一定有一些基本的东西我不明白。
一些代码:
using Azure.Messaging.ServiceBus;
string connectionString = "Endpoint=sb://xxxxxxxx";
string topicName = "topic";
string subscriptionName = "subscription";
await using var client = new ServiceBusClient(connectionString);
// Exception if subscription does not have any active messages...
ServiceBusSessionReceiver receiver = await client.AcceptNextSessionAsync(topicName, subscriptionName);
// Peek messages
IReadOnlyList<ServiceBusReceivedMessage> peekedMessages = await receiver.PeekMessagesAsync(10);
foreach (ServiceBusReceivedMessage peekedMessage in peekedMessagesFiltered)
{
Console.WriteLine(peekedMessage.EnqueuedTime.ToString());
}
最佳答案
- TimeoutExceptions 的想法是包含重试,这应该已经发生。日志条目中的这些异常本质上应该是暂时的,您的函数应该恢复并继续运行。
- 服务器未能在指定的时间段内响应请求的操作,该时间段由OperationTimeout 控制。请求的操作可能已由服务器完成。网络或其他基础设施的延迟可能会导致这种情况。
- 为了避免此类行为,您应该实现重试逻辑。
引用文档:Service Bus messaging exceptions - 请参阅以下链接,了解有关异常类型以及如何正确处理它们的更多信息 http://go.microsoft.com/fwlink/?LinkId=761101
- 检查 ServicePointManager.DefaultConnectionLimit 的值属性,因为超过它的限制可能会导致 TimeoutException。
- 引用文献 - link1 , link2 , link3
关于c# - 查看没有事件消息的 session 启用主题/订阅时出现超时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71786271/