繁体   English   中英

如何在Azure Service Bus Queue客户端中使用CancellationToken?

[英]How to use a CancellationToken with the Azure Service Bus Queue client?

在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);

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

你可能会像这样使用QueueClient.Abort

using (cancellationToken.Register(() => queueClient.Abort())
{
    var messages = await queueClient.ReceiveBatchAsync(
        10, TimeSpan.FromSeconds(30));
    return messages; // or process it 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM