Question

In the Azure Service Bus queue client, I use the ReceiveBatchAsync method to wait for a specified time to receive a batch of messages asynchronously.

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

I'd like a clean shutdown of my application, so I'm implementing CancellationToken on all of my long-running async processes, but there doesn't appear to be an overload of ReceiveBatchAsync that is cancelable.

In other words, I'd like to do this, but I can't:

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

What would be the best way to apply a CancellationToken to a task like this that doesn't offer it directly? I don't want to wait the entire 30 seconds during shutdown.

Was it helpful?

Solution

You probably could use QueueClient.Abort like this:

using (cancellationToken.Register(() => queueClient.Abort())
{
    var messages = await queueClient.ReceiveBatchAsync(
        10, TimeSpan.FromSeconds(30));
    return messages; // or process it 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top