This product is not supported for your selected Datadog site. ().
Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: csharp-best-practices/task-suppress-throwing

Language: C#

Severity: Notice

Category: Best Practices

Description

This rule prohibits using ConfigureAwaitOptions.SuppressThrowing directly on instances of Task<T>. The SuppressThrowing option is intended to be applied only on non-generic Task objects. Applying it on Task<T> can lead to unexpected behavior or runtime issues because Task<T> handles exceptions differently.

To comply with this rule, explicitly cast Task<T> instances to Task before calling ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing). For example: ((Task)t).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);. This approach ensures the suppress throwing option is applied correctly without affecting the generic task’s exception handling.

Non-Compliant Code Examples

Task<int> t = new Task<int>(() => 1);
(Task)t.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);

Compliant Code Examples

Task<int> t = new Task<int>(() => 1);
((Task)t).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains