sdk/tools/Azure.SdkAnalyzers/docs/AZC0020.md
A method that accepts a CancellationToken parameter calls an Azure SDK API with a RequestContext parameter, but does not propagate the cancellation token to the RequestContext.
In Azure SDK for .NET, many protocol-layer APIs accept a RequestContext parameter instead of a direct CancellationToken. Since RequestContext contains a CancellationToken property, callers must propagate any incoming token into the RequestContext passed to the SDK to ensure proper cancellation support.
Missing cancellation propagation can lead to:
Set the CancellationToken property on the RequestContext object to the incoming cancellation token:
public async Task UpdateAsync(CancellationToken cancellationToken)
{
await client.UpdateAsync(
content,
new RequestContext
{
CancellationToken = cancellationToken
});
}
Do not suppress warnings from this rule. Proper cancellation token propagation is critical for responsive applications and services.
public async Task UpdateAsync(CancellationToken cancellationToken)
{
// ❌ CancellationToken is accepted but not propagated
await client.UpdateAsync(
content,
new RequestContext()); // cancellationToken is dropped
}
public async Task UpdateAsync(CancellationToken cancellationToken)
{
// ✅ CancellationToken is properly propagated
await client.UpdateAsync(
content,
new RequestContext
{
CancellationToken = cancellationToken
});
}
Azure namespace)RequestContext is passed from a parameter or local variable, as it cannot determine if the token was already setCancellationToken to CancellationToken.None or default when a cancellation token parameter is available will still trigger a warning