span
span

Reputation: 5624

How to use cancellationToken "as necessary" with Azure Service Bus?

In the Azure Service Bus documentation there is a comment saying:

// Note: Use the cancellationToken passed as necessary to determine if the queueClient has already been closed.

// If queueClient has already been closed, you can choose to not call CompleteAsync() or AbandonAsync() etc.

// to avoid unnecessary exceptions.

I have been trying to find more information on how to use the token "as necessary" but it is not obvious to me. I tried reading the section on Task Cancellation but came out none the wiser.

The token has a few properties, CanBeCancelled and IsCancellationRequested that look interesting.

if (!token.IsCancellationRequested)
{
    await queueClient.CompleteAsync(message.SystemProperties.LockToken);
}

if (token.CanBeCanceled)
{
    await queueClient.CompleteAsync(message.SystemProperties.LockToken);
}

How do I properly use the token "as necessary" when receiving messages in peek lock mode?

Upvotes: 0

Views: 329

Answers (1)

PramodValavala
PramodValavala

Reputation: 6647

The IsCancellationRequested property is the one that you are looking for and the if statement that you've shared is how you should use it.

Also, you could make the same check before starting any long running process as well since the message would be reprocessed anyways I suppose.

Upvotes: 1

Related Questions