CancelCop.Analyzer 1.1.2

dotnet add package CancelCop.Analyzer --version 1.1.2
                    
NuGet\Install-Package CancelCop.Analyzer -Version 1.1.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CancelCop.Analyzer" Version="1.1.2">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CancelCop.Analyzer" Version="1.1.2" />
                    
Directory.Packages.props
<PackageReference Include="CancelCop.Analyzer">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CancelCop.Analyzer --version 1.1.2
                    
#r "nuget: CancelCop.Analyzer, 1.1.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CancelCop.Analyzer@1.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CancelCop.Analyzer&version=1.1.2
                    
Install as a Cake Addin
#tool nuget:?package=CancelCop.Analyzer&version=1.1.2
                    
Install as a Cake Tool

CancelCop Analyzer

A surgical Roslyn analyzer focused on CancellationToken propagation and honoring across public APIs, handlers, EF Core, HTTP calls, and Minimal APIs, with comprehensive automatic code fixes.

Why CancelCop?

CancelCop helps you build responsive, cancellable .NET applications by ensuring CancellationToken is properly used throughout your codebase. It detects missing tokens and provides automatic fixes with a single click.

Features

8 Comprehensive Analyzers

Rule Description Severity
CC001 Public async methods must have CancellationToken parameter Warning
CC002 CancellationToken must be propagated to async calls Warning
CC003 EF Core queries must pass CancellationToken Warning
CC004 HttpClient methods must pass CancellationToken Warning
CC005A MediatR handlers must accept CancellationToken Warning
CC005B Controller actions must accept CancellationToken Warning
CC005C Minimal API endpoints must accept CancellationToken Warning
CC006 CancellationToken should be the last parameter Info

Automatic Code Fixes

  • ✅ Adds CancellationToken parameters to method signatures
  • ✅ Propagates tokens to inner async calls
  • ✅ Handles lambda expressions (Minimal APIs)
  • ✅ Smart using directive management
  • ✅ Preserves code formatting

Quick Start

Installation

dotnet add package CancelCop.Analyzer

Usage

Once installed, the analyzer runs automatically during build. It will:

  1. Detect CancellationToken issues in your code
  2. Show warnings in your IDE with clear descriptions
  3. Offer code fixes - just click the lightbulb 💡

Example

// ❌ Before (CC001 warning)
public async Task ProcessDataAsync()
{
    await Task.Delay(100);
}

// ✅ After (automatic fix applied)
public async Task ProcessDataAsync(CancellationToken cancellationToken = default)
{
    await Task.Delay(100, cancellationToken);
}

Supported Patterns

Entity Framework Core

// Detects missing tokens in:
await _context.Users.ToListAsync(cancellationToken);
await _context.Users.FirstOrDefaultAsync(u => u.Id == id, cancellationToken);
await _context.SaveChangesAsync(cancellationToken);

HttpClient

// Detects missing tokens in:
await httpClient.GetAsync(url, cancellationToken);
await httpClient.PostAsync(url, content, cancellationToken);
await httpClient.SendAsync(request, cancellationToken);

ASP.NET Core Controllers

[HttpGet]
public async Task<IActionResult> GetUsers(CancellationToken cancellationToken)
{
    var users = await _service.GetUsersAsync(cancellationToken);
    return Ok(users);
}

Minimal APIs

app.MapGet("/users", async (CancellationToken ct) =>
    await GetUsersAsync(ct));

MediatR Handlers

public class MyHandler : IRequestHandler<MyRequest, MyResponse>
{
    public async Task<MyResponse> Handle(
        MyRequest request,
        CancellationToken cancellationToken)
    {
        // ...
    }
}

Benefits

  • 🎯 Responsive Applications: Properly cancel long-running operations
  • 🚀 Better Performance: Avoid wasting resources on cancelled operations
  • 🛡️ Production Ready: Reduce timeout issues and improve reliability
  • Developer Friendly: Automatic fixes save time and reduce errors
  • 📊 Comprehensive: Covers all major .NET async patterns

Configuration

All rules are enabled by default with appropriate severity levels. You can configure them in .editorconfig:

[*.cs]
# Adjust severity (none, suggestion, warning, error)
dotnet_diagnostic.CC001.severity = warning
dotnet_diagnostic.CC006.severity = suggestion

Test Coverage

  • 82 tests ensuring reliability
  • All analyzers and code fixes thoroughly tested
  • Covers edge cases and complex scenarios

Learn More


Built with ❤️ using Roslyn and following TDD best practices.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.2 152 10/1/2025
1.1.1 141 10/1/2025
1.1.0 146 10/1/2025