6

在Azure Service Bus队列客户端中,我使用了ReceiveBatchAsync等待指定时间异步接收一批消息的方法。

var messages = await queueClient.ReceiveBatchAsync(10, TimeSpan.FromSeconds(30));

我想彻底关闭我的应用程序,所以我CancellationToken在所有长时间运行的异步进程上实现,但似乎没有可以ReceiveBatchAsync取消的过载。

换句话说,我想这样做,但我不能:

var messages = await queueClient.ReceiveBatchAsync(10, TimeSpan.FromSeconds(30),
                                                       cancellationToken);

将 a 应用于CancellationToken不直接提供它的此类任务的最佳方法是什么?我不想在关机期间等待整个 30 秒。

4

1 回答 1

3

你可能可以这样使用QueueClient.Abort

using (cancellationToken.Register(() => queueClient.Abort())
{
    var messages = await queueClient.ReceiveBatchAsync(
        10, TimeSpan.FromSeconds(30));
    return messages; // or process it 
}
于 2014-05-15T04:41:11.840 回答