# AsyncAwaitBestPractices for safer C# task execution and MVVM commands

> AsyncAwaitBestPractices is a .NET library for handling Task and ValueTask operations that might otherwise be left unawaited, plus an MVVM package for asynchronous ICommand implementations. It targets C# developers working with asynchronous code and UI command patterns.

**TheCodeTraveler/AsyncAwaitBestPractices** — Extensions for System.Threading.Tasks.Task and System.Threading.Tasks.ValueTask

- Repository: https://github.com/TheCodeTraveler/AsyncAwaitBestPractices
- Website: https://www.nuget.org/packages/AsyncAwaitBestPractices.MVVM/
- Stars: 1,843 · Forks: 173
- Language: C#
- License: MIT
- Published: 2026-10-16 · Updated: 2026-10-16 · Language: en
- Canonical page: https://hysenlabs.com/projects/thecodetraveler-asyncawaitbestpractices

## Why AsyncAwaitBestPractices targets unawaited work

AsyncAwaitBestPractices addresses two recurring problems in C# asynchronous code: operations that execute concurrently in an unexpected order, and exceptions that become difficult for application code to observe. The repository focuses on the gap between an asynchronous method being available and its Task actually being awaited. A non-awaited Task does not automatically rethrow an exception in the way the library's `SafeFireAndForget` extension is intended to address.

The project is aimed at developers who already use `System.Threading.Tasks.Task` or `System.Threading.Tasks.ValueTask` and need help containing the consequences of asynchronous work that must start without being awaited immediately. That can include background maintenance initiated from an event handler, a command, or another operation whose caller does not have a natural continuation. The library does not make asynchronous code synchronous, and it does not replace application-level cancellation, concurrency, or exception policy. It supplies a specific mechanism for observing work that might otherwise be abandoned.

The library is also relevant to MVVM applications because UI frameworks commonly bind actions through `ICommand`. An ordinary command delegate is synchronous, while the work triggered by it may not be. The `AsyncAwaitBestPractices.MVVM` package adapts Task and ValueTask operations to command abstractions designed for asynchronous execution.

## SafeFireAndForget and the compiler-generated async state machine

The README explains that the C# compiler transforms an `async` method into an `IAsyncStateMachine`. Its `MoveNext()` method runs until the method reaches an `await`, then yields control while the awaited operation proceeds. The state machine lets the runtime remember which part of the method must resume later.

That mechanism also explains the exception problem addressed by the library. The generated `MoveNext()` implementation is wrapped in a `try/catch` block, so exceptions raised inside the asynchronous method are captured by the generated state machine and associated with the returned task. If a caller does not await that task, the exception may not surface through the normal calling path. The extension observes the task and accepts an `onException` callback; a default handler can also be configured. Whether an exception is rethrown depends on configuration. The project warns against forcing all exceptions to rethrow in production.

This is an observation and dispatch mechanism, not automatic error recovery. It does not define what should happen after an error, whether a retry is appropriate, or whether the caller should cancel related work. The documentation's example is centered on making an otherwise unawaited task observable. Teams still need to decide where exceptions should be routed and whether duplicate execution is possible.

## WeakEventManager and the lifetime of MVVM event handlers

`WeakEventManager` is the base package feature associated with memory-leak avoidance when event subscriptions are not explicitly removed. The README says it is used by `AsyncCommand`, `AsyncCommand<T>`, `AsyncValueCommand`, and `AsyncValueCommand<T>`. This places weak event handling inside the command layer rather than presenting it as a separate general-purpose application utility.

For MVVM code, event subscriptions can keep a view model alive when a publisher retains the subscriber. The command implementations therefore have a documented connection to `WeakEventManager`, but the README does not specify the exact subscription algorithm, the lifetime guarantees, or the behavior for every event source. That missing detail matters when an application depends on deterministic disposal. Weak references can reduce retention pressure, but they do not automatically guarantee that a command is disposed, that a view is collected at a particular time, or that event handlers stop producing side effects. Those are runtime and framework questions that must be checked in the target application.

## AsyncAwaitBestPractices.MVVM command types and parameter variants

The MVVM package separates command APIs for `Task` and `ValueTask`. For Task-based execution, the README lists `IAsyncCommand`, `AsyncCommand`, `IAsyncCommand<T>`, `AsyncCommand<T>`, `IAsyncCommand<TExecute, TCanExecute>`, and `AsyncCommand<TExecute, TCanExecute>`. Each interface inherits from `ICommand`, as stated in the repository documentation.

The package provides corresponding ValueTask interfaces and classes: `IAsyncValueCommand`, `AsyncValueCommand`, `IAsyncValueCommand<T>`, `AsyncValueCommand<T>`, `IAsyncValueCommand<TExecute, TCanExecute>`, and `AsyncValueCommand<TExecute, TCanExecute>`. This gives view models a choice between the two async result abstractions rather than forcing every command through the same API surface.

The distinction matters for allocation and usage choices, although this README does not provide performance measurements or prescribe when to select one type over the other. The documented design choice is clearer than the sizing guidance: Task-oriented command types use Task, while ValueTask-oriented command types use ValueTask. Applications should select the family that matches the surrounding code and then verify how command re-entry, exceptions, and completion are handled by the selected version.

## NuGet setup and the first command integration

The README says that `AsyncAwaitBestPractices` and `AsyncAwaitBestPractices.MVVM` are available on NuGet and can be added to any project supporting.NET Standard 1.0.

For a first real use, begin by choosing the package that matches the feature you need. Use `AsyncAwaitBestPractices` for `SafeFireAndForget` or `WeakEventManager`. Use `AsyncAwaitBestPractices.MVVM` when an MVVM view model needs asynchronous `ICommand` types. The repository identifies both packages separately, so adding both without a corresponding requirement would make the dependency boundary less clear.

After installation, inspect the package and project API surface in the target.NET solution, then wire the command implementation to the UI framework's existing view-model binding. The README provides `AsyncCommand` constructor signatures and examples, including exception handling and `CanExecute` changes; consult the sample for platform-specific UI binding and startup configuration. The available sample directory contains a.NET MAUI-oriented sample, including `MauiProgram.cs` and view-model files. Those files are the first place to verify integration details for an application that follows the sample's platform assumptions.

## Maintenance, licensing, and upgrade boundaries

The repository is not archived, and the recorded last push was on 2026-10-15. The recent release list includes v10.0.0 dated 2025-11-11, v8.0.0 dated 2024-07-09, and 7.0.0 dated 2023-11-14. The README identifies the project as available through NuGet, while the repository metadata identifies the default branch as `main`.

The license is MIT. That designation permits use and modification under the license's stated conditions, but this review does not provide legal advice or reproduce license obligations. For an upgrade, the important project-specific boundary is that `AsyncAwaitBestPractices` and `AsyncAwaitBestPractices.MVVM` are separate packages with different APIs. Review the target package version and the command constructor and interface signatures before changing an existing view model.

The README also says the core package can be added to any project supporting .NET Standard 1.0. That broad compatibility statement does not by itself confirm behavior on every current runtime, UI framework, or command binding implementation. Compatibility claims should be checked against the application's target framework and the package documentation for the version being adopted.

## Conclusion

Adopt AsyncAwaitBestPractices when your C# application has fire-and-forget work or MVVM commands that need Task or ValueTask support, but first verify the package choice, exception visibility, event subscriptions, and the behavior of your target UI command framework. It is a poor fit if every asynchronous operation can be awaited directly or if you need a complete MVVM framework rather than a focused set of command and task extensions.

## FAQ

### What does AsyncAwaitBestPractices.SafeFireAndForget do?

SafeFireAndForget observes an unawaited Task or ValueTask and supports an onException callback and a default exception handler. Rethrowing depends on the configured behavior; the project warns against forcing all exceptions to rethrow in production. Choose an explicit error-handling policy and await operations when possible.

### Does AsyncAwaitBestPractices.MVVM support both Task and ValueTask commands?

Yes. The README lists separate Task-based and ValueTask-based interfaces and classes, including parameter and separate execute, can-execute variants. All listed interfaces inherit from ICommand.

### Where is AsyncAwaitBestPractices installed from?

Both AsyncAwaitBestPractices and AsyncAwaitBestPractices.MVVM are documented as available on NuGet. The README says they can be added to projects supporting .NET Standard 1.0, but it does not provide an exact installation command.

## Sources

- [License: MIT](https://github.com/TheCodeTraveler/AsyncAwaitBestPractices/blob/main/LICENSE)
- [Project website](https://www.nuget.org/packages/AsyncAwaitBestPractices.MVVM/)
- [README](https://github.com/TheCodeTraveler/AsyncAwaitBestPractices/blob/main/README.md)
- [Releases](https://github.com/TheCodeTraveler/AsyncAwaitBestPractices/releases)
- [TheCodeTraveler/AsyncAwaitBestPractices on GitHub](https://github.com/TheCodeTraveler/AsyncAwaitBestPractices)

---

Hysen Labs editorial analysis, written from the project's own repository and release notes. Cite the canonical page: https://hysenlabs.com/projects/thecodetraveler-asyncawaitbestpractices
