cancellation-tokens-ai-kit

Propagate CancellationToken through .NET async workflows and HTTP requests.

Updated May 11, 2026
One-click install
npx skills add https://github.com/ducthang-hub/nw-ai-kit --skill cancellation-tokens-ai-kit
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cancellation-tokens-ai-kit
Source: https://github.com/ducthang-hub/nw-ai-kit/tree/main/.agents/skills/cancellation-tokens-ai-kit
Command: npx skills add https://github.com/ducthang-hub/nw-ai-kit --skill cancellation-tokens-ai-kit

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It helps you implement reliable cancellation in .NET so long-running, async, and request-scoped operations stop promptly without leaving your app in an inconsistent state.

Core Features & Use Cases

  • Request-scoped cancellation: Use HttpContext.RequestAborted as the upstream CancellationToken for web endpoints.
  • Correct propagation: Pass the same CancellationToken through controllers, services, repositories, and EF Core async calls.
  • Loop and background safety: Check IsCancellationRequested or call ThrowIfCancellationRequested inside long-running loops and handle OperationCanceledException appropriately.

Quick Start

Use the cancellation-tokens-ai-kit skill to show you how to wire HttpContext.RequestAborted into your controller and propagate the token down to your EF Core queries and background loops.

Frequently Asked Questions about cancellation-tokens-ai-kit

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I propagate CancellationToken through .NET async workflows?

To propagate CancellationToken through .NET async workflows, pass the same token down from controllers to services, repositories, and EF Core async calls. This ensures long-running operations stop promptly without leaving your app in an inconsistent state.

How do I use HttpContext.RequestAborted to cancel EF Core queries?

HttpContext.RequestAborted provides the upstream CancellationToken for web endpoints. You wire this token into your controller and pass it directly to your EF Core async calls to safely cancel database queries when the client disconnects.

What is the best way to handle OperationCanceledException in .NET background services?

The best way to handle OperationCanceledException in .NET background services is to catch it during graceful stopping. Use explicit IsCancellationRequested checks inside long-running loops to ensure background operations terminate safely.

Can I use CancellationTokenSource for timeouts in HttpClient calls?

Yes, you can use CancellationTokenSource for timeouts in HttpClient calls. The skill applies correct token propagation and explicit cancellation checks to safely handle timed-out HTTP requests and downstream async operations.

Why does my long-running async loop not stop when the request is aborted?

Your long-running async loop does not stop because the CancellationToken is not checked or propagated. You must call ThrowIfCancellationRequested inside the loop and pass the token to async methods like Task.Delay to ensure prompt termination.

Do I need to check IsCancellationRequested in addition to passing the CancellationToken?

Yes, you need to check IsCancellationRequested or call ThrowIfCancellationRequested in addition to passing the token. Explicit checks are required inside long-running loops to guarantee immediate cancellation between asynchronous operations.