dotnet-async-patterns

Optimize .NET async code with TAP, ValueTask, and cancellation tokens.

24|5|Updated Nov 28, 2025
One-click install
npx skills add https://github.com/thapaliyabikendra/ai-artifacts --skill dotnet-async-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-async-patterns
Source: https://github.com/thapaliyabikendra/ai-artifacts/tree/main/.claude/skills/dotnet-async-patterns
Command: npx skills add https://github.com/thapaliyabikendra/ai-artifacts --skill dotnet-async-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you master asynchronous programming in .NET, enabling you to build high-performance, scalable, and responsive ASP.NET Core applications. It addresses common pitfalls like deadlocks, thread pool starvation, and inefficient I/O operations, ensuring your applications remain fluid under load.

Core Features & Use Cases

  • Async/Await Fundamentals: Guides on correctly implementing and calling async/await methods, avoiding common blocking patterns that lead to deadlocks.
  • ConfigureAwait & ValueTask: Explains the nuances of ConfigureAwait(false) for library code and when to use ValueTask for performance optimization in hot paths.
  • Cancellation Tokens: Demonstrates how to integrate CancellationToken throughout your async operations, allowing for graceful termination of long-running tasks.
  • Parallel Processing: Patterns for efficiently executing multiple independent async operations concurrently using Task.WhenAll and Task.WhenAny, improving overall application speed.
  • Use Case: A backend developer needs to optimize an ASP.NET Core API endpoint that makes multiple external service calls. Using this skill, they refactor the endpoint to use Task.WhenAll to execute calls in parallel and integrate CancellationToken for better responsiveness.

Quick Start

Refactor a synchronous method that calls two independent database operations into an asynchronous method using Task.WhenAll to execute them in parallel.

Frequently Asked Questions about dotnet-async-patterns

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

FAQPage Schema
How do I prevent deadlocks when using async/await in .NET applications?

Deadlocks occur when async code blocks threads waiting for results on the synchronization context. Use `ConfigureAwait(false)` in library code and avoid blocking calls like `.Result` or `.Wait()` on tasks. Always await async methods instead of blocking to keep the context free.

When should I use ValueTask instead of Task in .NET?

ValueTask reduces allocation overhead in hot paths where methods frequently complete synchronously. Use ValueTask for performance-critical async methods that often return completed results, such as cached database lookups or fast I/O operations.

How do I run multiple independent async operations in parallel with Task.WhenAll?

Task.WhenAll executes multiple async tasks concurrently and waits for all to complete. Call `await Task.WhenAll(task1, task2, task3)` to parallelize independent operations like external service calls, improving overall throughput and responsiveness.

How do I implement CancellationToken for graceful termination of long-running async operations?

Pass CancellationToken through your async method chain and check `token.ThrowIfCancellationRequested()` at key points. When a cancellation is signaled, the token allows clean shutdown of long-running tasks without abrupt termination.

Can I use async/await patterns with ASP.NET Core APIs and Entity Framework Core?

Yes. ASP.NET Core and EF Core are built for async operations. Use `async/await` on controller actions and `await` on EF Core queries like `dbContext.Users.ToListAsync()` to avoid blocking thread pool threads and improve API scalability.

What's the difference between Task.WhenAll and Task.WhenAny for concurrent async operations?

Task.WhenAll waits for all tasks to complete; Task.WhenAny returns when the first task completes. Use WhenAll for operations that must all succeed, and WhenAny when you need the fastest result or first success from multiple options.