csharp-async-patterns

Implements async/await patterns, cancellation tokens, and error handling for C# asynchronous code.

Updated Aug 10, 2025
One-click install
npx skills add https://github.com/afonsoft/ai-studio-workspace --skill csharp-async-patterns-afonsoft
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: csharp-async-patterns
Source: https://github.com/afonsoft/ai-studio-workspace/tree/main/agents/skills/csharp-async-patterns
Command: npx skills add https://github.com/afonsoft/ai-studio-workspace --skill csharp-async-patterns-afonsoft

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing asynchronous C# code often leads to deadlocks, unhandled exceptions, and performance bottlenecks when developers misuse async/await, block on Tasks, or ignore cancellation support. ## Core Features & Use Cases - Async Method Implementation: Convert synchronous methods to async with proper Task return types, exception handling, and ConfigureAwait guidance. - Advanced Patterns: Apply async streams (IAsyncEnumerable), ValueTask for hot paths, retry logic with exponential backoff, and async batching with SemaphoreSlim. - Pitfall Avoidance: Identify and fix async void methods, blocking calls like .Result, and fire-and-forget tasks without error handling. - Use Case: When building an ASP.NET Core service that calls a database and external APIs, use this Skill to add cancellation token support, retry transient HTTP failures, and stream large result sets efficiently. ## Quick Start Review my C# service class and convert its synchronous data access methods to async with proper error handling and cancellation token support.

Frequently Asked Questions about csharp-async-patterns

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

FAQPage Schema
How do I convert a synchronous C# method to async?▼

Identify I/O operations, add the async keyword, await the asynchronous calls, and return Task or Task<T>. Handle exceptions with try/catch, accept a CancellationToken parameter, and avoid blocking calls like .Result or .Wait().

When should I use ConfigureAwait(false) in C#?▼

Use ConfigureAwait(false) in library code where the continuation does not need the original synchronization context. In application code such as ASP.NET controllers, it can be omitted since context flow is typically desired.

What is the difference between Task and ValueTask in C#?▼

ValueTask is a lightweight alternative for frequently called async operations that often complete synchronously, reducing heap allocations. Use it for hot paths like cached lookups; otherwise, Task remains the default choice.

Why is async void considered bad practice in C#?▼

Async void methods cannot be awaited, so exceptions escape the caller's try/catch and can crash the process. They should only be used for event handlers; all other async methods should return Task.

How do I add cancellation support to async C# methods?▼

Accept a CancellationToken parameter with a default value, pass it to downstream async calls, and call ThrowIfCancellationRequested in loops. Callers can then cancel via CancellationTokenSource, for example with a timeout.

How do I test async methods with xUnit?▼

Make the test method async Task and await the call under test. Use ReturnsAsync and ThrowsAsync with Moq for mocked dependencies, and Assert.ThrowsAsync to verify exception propagation.