문제

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.

도움이 되었습니까?

해결책

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 
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top