A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2261 below:

CA2261: Do not use `ConfigureAwaitOptions.SuppressThrowing` with `Task<TResult>` - .NET

CA2261: Do not use ConfigureAwaitOptions.SuppressThrowing with Task<TResult>

In this article Property Value Rule ID CA2261 Title Do not use ConfigureAwaitOptions.SuppressThrowing with Task<TResult> Category Usage Fix is breaking or non-breaking Non-breaking Enabled by default in .NET 9 As warning Cause

A value of ConfigureAwaitOptions.SuppressThrowing is passed to Task<TResult>.ConfigureAwait(ConfigureAwaitOptions).

Rule description

The ConfigureAwaitOptions.SuppressThrowing option isn't supported by the generic Task<TResult>, since that might lead to returning an invalid TResult. This rule flags the use of SuppressThrowing with Task<TResult> to surface the error at build time rather than run time.

How to fix violations

Cast the Task<TResult> to a non-generic Task before calling ConfigureAwait(ConfigureAwaitOptions).

Example

The following code snippet shows a violation of CA2261:

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

The following code snippet shows the fix:

Task<int> t = new Task<int>(() => 1);
((Task)t).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
When to suppress errors

You shouldn't suppress warnings from this rule. If the task is faulted or canceled, TResult will be invalid and cause run-time errors.

Collaborate with us on GitHub

The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.

Additional resources

In this article

Was this page helpful?


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4