TastenTiger.Analyzers.Async.EnforceCancellationToken
1.0.1
Requires NuGet 2.12 or higher.
dotnet add package TastenTiger.Analyzers.Async.EnforceCancellationToken --version 1.0.1
NuGet\Install-Package TastenTiger.Analyzers.Async.EnforceCancellationToken -Version 1.0.1
<PackageReference Include="TastenTiger.Analyzers.Async.EnforceCancellationToken" Version="1.0.1" />
<PackageVersion Include="TastenTiger.Analyzers.Async.EnforceCancellationToken" Version="1.0.1" />
<PackageReference Include="TastenTiger.Analyzers.Async.EnforceCancellationToken" />
paket add TastenTiger.Analyzers.Async.EnforceCancellationToken --version 1.0.1
#r "nuget: TastenTiger.Analyzers.Async.EnforceCancellationToken, 1.0.1"
#:package TastenTiger.Analyzers.Async.EnforceCancellationToken@1.0.1
#addin nuget:?package=TastenTiger.Analyzers.Async.EnforceCancellationToken&version=1.0.1
#tool nuget:?package=TastenTiger.Analyzers.Async.EnforceCancellationToken&version=1.0.1
TastenTiger.Analyzers.Async.EnforceCancellationToken
A Roslyn analyzer that ensures asynchronous methods, those returning Task or ValueTask, include a
CancellationToken as their final parameter.
Why enforce CancellationToken usage?
Asynchronous operations often involve I/O, long-running work, or external dependencies. When a caller no longer needs the result, continuing the operation wastes CPU time, memory, and other server resources.
Most asynchronous .NET Methods already include a CancellationToken as their last parameter. This analyzer enforces
this best practice via compilation warning.
Requiring a CancellationToken:
- Allows callers to cancel work early
- Reduces an unnecessary load on your system
- Encourages predictable async APIs
- Enforces best practices
What this analyzer does
- Produces the
TA0001compilation warning for methods that- use the
asynckeyword - return a
Task - return a
ValueTask
- use the
- Provides a bulk quick fix to automatically add
System.Threading.CancellationToken cancellationToken = defaultparameter to the end of the parameter list
Examples
public async Task DoWorkAsync() // <-- violating TA0001
{
}
public async Task DoWorkAsync(CancellationToken cancellationToken = default) // <-- complying TA0001
{
}
For more examples see
TastenTiger.Analyzers.Async.EnforceCancellationToken.Sample.Examples.cs
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
GitHub repositories
This package is not used by any popular GitHub repositories.