VOOZH about

URL: https://www.nuget.org/packages/AsyncAwaitBestPractices/

⇱ NuGet Gallery | AsyncAwaitBestPractices 10.0.0




AsyncAwaitBestPractices 10.0.0

dotnet add package AsyncAwaitBestPractices --version 10.0.0
 
 
NuGet\Install-Package AsyncAwaitBestPractices -Version 10.0.0
 
 
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="AsyncAwaitBestPractices" Version="10.0.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AsyncAwaitBestPractices" Version="10.0.0" />
 
Directory.Packages.props
<PackageReference Include="AsyncAwaitBestPractices" />
 
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 AsyncAwaitBestPractices --version 10.0.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AsyncAwaitBestPractices, 10.0.0"
 
 
#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 AsyncAwaitBestPractices@10.0.0
 
 
#: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=AsyncAwaitBestPractices&version=10.0.0
 
Install as a Cake Addin
#tool nuget:?package=AsyncAwaitBestPractices&version=10.0.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

AsyncAwaitBestPractices

👁 NuGet

Available on NuGet: https://www.nuget.org/packages/AsyncAwaitBestPractices/

  • SafeFireAndForget
    • An extension method to safely fire-and-forget a Task or a ValueTask
    • Ensures the Task will rethrow an Exception if an Exception is caught in IAsyncStateMachine.MoveNext()
  • WeakEventManager
    • Avoids memory leaks when events are not unsubscribed
    • Used by AsyncCommand, AsyncCommand<T>, AsyncValueCommand, AsyncValueCommand<T>
  • Usage instructions

Setup

Usage

SafeFireAndForget

An extension method to safely fire-and-forget a Task.

SafeFireAndForget allows a Task to safely run on a different thread while the calling thread does not wait for its completion.

public static async void SafeFireAndForget(this System.Threading.Tasks.Task task, System.Action<System.Exception>? onException = null, bool continueOnCapturedContext = false)
public static async void SafeFireAndForget(this System.Threading.Tasks.ValueTask task, System.Action<System.Exception>? onException = null, bool continueOnCapturedContext = false)
Basic Usage - Task
void HandleButtonTapped(object sender, EventArgs e)
{
 // Allows the async Task method to safely run on a different thread while the calling thread continues, not awaiting its completion
 // onException: If an Exception is thrown, print it to the Console
 ExampleAsyncMethod().SafeFireAndForget(onException: ex => Console.WriteLine(ex));

 // HandleButtonTapped continues execution here while `ExampleAsyncMethod()` is running on a different thread
 // ...
}

async Task ExampleAsyncMethod()
{
 await Task.Delay(1000);
}
Basic Usage - ValueTask

If you're new to ValueTask, check out this great write-up, Understanding the Whys, Whats, and Whens of ValueTask.

void HandleButtonTapped(object sender, EventArgs e)
{
 // Allows the async ValueTask method to safely run on a different thread while the calling thread continues, not awaiting its completion
 // onException: If an Exception is thrown, print it to the Console
 ExampleValueTaskMethod().SafeFireAndForget(onException: ex => Console.WriteLine(ex));

 // HandleButtonTapped continues execution here while `ExampleAsyncMethod()` is running on a different thread
 // ...
}

async ValueTask ExampleValueTaskMethod()
{
 var random = new Random();
 if (random.Next(10) > 9)
 await Task.Delay(1000);
}
Advanced Usage
void InitializeSafeFireAndForget()
{
 // Initialize SafeFireAndForget
 // Only use `shouldAlwaysRethrowException: true` when you want `.SafeFireAndForget()` to always rethrow every exception. This is not recommended, because there is no way to catch an Exception rethrown by `SafeFireAndForget()`; `shouldAlwaysRethrowException: true` should **not** be used in Production/Release builds.
 SafeFireAndForgetExtensions.Initialize(shouldAlwaysRethrowException: false);

 // SafeFireAndForget will print every exception to the Console
 SafeFireAndForgetExtensions.SetDefaultExceptionHandling(ex => Console.WriteLine(ex));
}

void UninitializeSafeFireAndForget()
{
 // Remove default exception handling
 SafeFireAndForgetExtensions.RemoveDefaultExceptionHandling()
}

void HandleButtonTapped(object sender, EventArgs e)
{
 // Allows the async Task method to safely run on a different thread while not awaiting its completion
 // onException: If a WebException is thrown, print its StatusCode to the Console. **Note**: If a non-WebException is thrown, it will not be handled by `onException`
 // Because we set `SetDefaultExceptionHandling` in `void InitializeSafeFireAndForget()`, the entire exception will also be printed to the Console
 ExampleAsyncMethod().SafeFireAndForget<WebException>(onException: ex =>
 {
 if(ex.Response is HttpWebResponse webResponse)
 Console.WriteLine($"Task Exception\n Status Code: {webResponse.StatusCode}");
 });
 
 ExampleValueTaskMethod().SafeFireAndForget<WebException>(onException: ex =>
 {
 if(ex.Response is HttpWebResponse webResponse)
 Console.WriteLine($"ValueTask Error\n Status Code: {webResponse.StatusCode}");
 });

 // HandleButtonTapped continues execution here while `ExampleAsyncMethod()` and `ExampleValueTaskMethod()` run in the background
}

async Task ExampleAsyncMethod()
{
 await Task.Delay(1000);
 throw new WebException();
}

async ValueTask ExampleValueTaskMethod()
{
 var random = new Random();
 if (random.Next(10) > 9)
 await Task.Delay(1000);
 
 throw new WebException();
}

WeakEventManager

An event implementation that enables the garbage collector to collect an object without needing to unsubscribe event handlers.

Inspired by Xamarin.Forms.WeakEventManager.

Using EventHandler
readonly WeakEventManager _canExecuteChangedEventManager = new WeakEventManager();

public event EventHandler CanExecuteChanged
{
 add => _canExecuteChangedEventManager.AddEventHandler(value);
 remove => _canExecuteChangedEventManager.RemoveEventHandler(value);
}

void OnCanExecuteChanged() => _canExecuteChangedEventManager.RaiseEvent(this, EventArgs.Empty, nameof(CanExecuteChanged));
Using Delegate
readonly WeakEventManager _propertyChangedEventManager = new WeakEventManager();

public event PropertyChangedEventHandler PropertyChanged
{
 add => _propertyChangedEventManager.AddEventHandler(value);
 remove => _propertyChangedEventManager.RemoveEventHandler(value);
}

void OnPropertyChanged([CallerMemberName]string propertyName = "") => _propertyChangedEventManager.RaiseEvent(this, new PropertyChangedEventArgs(propertyName), nameof(PropertyChanged));
Using Action
readonly WeakEventManager _weakActionEventManager = new WeakEventManager();

public event Action ActionEvent
{
 add => _weakActionEventManager.AddEventHandler(value);
 remove => _weakActionEventManager.RemoveEventHandler(value);
}

void OnActionEvent(string message) => _weakActionEventManager.RaiseEvent(message, nameof(ActionEvent));

WeakEventManager<T>

An event implementation that enables the garbage collector to collect an object without needing to unsubscribe event handlers.

Inspired by Xamarin.Forms.WeakEventManager.

Using EventHandler<T>
readonly WeakEventManager<string> _errorOcurredEventManager = new WeakEventManager<string>();

public event EventHandler<string> ErrorOcurred
{
 add => _errorOcurredEventManager.AddEventHandler(value);
 remove => _errorOcurredEventManager.RemoveEventHandler(value);
}

void OnErrorOcurred(string message) => _errorOcurredEventManager.RaiseEvent(this, message, nameof(ErrorOcurred));
Using Action<T>
readonly WeakEventManager<string> _weakActionEventManager = new WeakEventManager<string>();

public event Action<string> ActionEvent
{
 add => _weakActionEventManager.AddEventHandler(value);
 remove => _weakActionEventManager.RemoveEventHandler(value);
}

void OnActionEvent(string message) => _weakActionEventManager.RaiseEvent(message, nameof(ActionEvent));
Product Versions Compatible and additional computed target framework versions.
.NET net5.0 net5.0 was computed.  net5.0-windows net5.0-windows was computed.  net6.0 net6.0 was computed.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 was computed.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 is compatible.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 is compatible.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 is compatible.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
.NET Core netcoreapp1.0 netcoreapp1.0 was computed.  netcoreapp1.1 netcoreapp1.1 was computed.  netcoreapp2.0 netcoreapp2.0 was computed.  netcoreapp2.1 netcoreapp2.1 was computed.  netcoreapp2.2 netcoreapp2.2 was computed.  netcoreapp3.0 netcoreapp3.0 was computed.  netcoreapp3.1 netcoreapp3.1 was computed. 
.NET Standard netstandard1.0 netstandard1.0 is compatible.  netstandard1.1 netstandard1.1 was computed.  netstandard1.2 netstandard1.2 was computed.  netstandard1.3 netstandard1.3 was computed.  netstandard1.4 netstandard1.4 was computed.  netstandard1.5 netstandard1.5 was computed.  netstandard1.6 netstandard1.6 was computed.  netstandard2.0 netstandard2.0 is compatible.  netstandard2.1 netstandard2.1 is compatible. 
.NET Framework net45 net45 was computed.  net451 net451 was computed.  net452 net452 was computed.  net46 net46 was computed.  net461 net461 was computed.  net462 net462 was computed.  net463 net463 was computed.  net47 net47 was computed.  net471 net471 was computed.  net472 net472 was computed.  net48 net48 was computed.  net481 net481 was computed. 
MonoAndroid monoandroid monoandroid was computed. 
MonoMac monomac monomac was computed. 
MonoTouch monotouch monotouch was computed. 
Tizen tizen30 tizen30 was computed.  tizen40 tizen40 was computed.  tizen60 tizen60 was computed. 
Universal Windows Platform uap uap was computed.  uap10.0 uap10.0 was computed. 
Windows Phone wp8 wp8 was computed.  wp81 wp81 was computed.  wpa81 wpa81 was computed. 
Windows Store netcore netcore was computed.  netcore45 netcore45 was computed.  netcore451 netcore451 was computed. 
Xamarin.iOS xamarinios xamarinios was computed. 
Xamarin.Mac xamarinmac xamarinmac was computed. 
Xamarin.TVOS xamarintvos xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (30)

Showing the top 5 NuGet packages that depend on AsyncAwaitBestPractices:

Package Downloads
Mopups

Popups for MAUI

AsyncAwaitBestPractices.MVVM

Async Extensions for ICommand Includes AsyncCommand and IAsyncCommand which allows ICommand to safely be used asynchronously with Task. Includes AsyncValueCommand and IAsyncValueCommand which allows ICommand to safely be used asynchronously with ValueTask

Mopups.Maui

Popups for MAUI

Shinya.Core

Shinya.Framework

Sanet.MakaMek.Core

An Attempt of Classic BattleTech game implementation. Game Logic

GitHub repositories (24)

Showing the top 20 popular GitHub repositories that depend on AsyncAwaitBestPractices:

Repository Stars
LykosAI/StabilityMatrix
Multi-Platform Package Manager for Stable Diffusion
beeradmoore/dlss-swapper
dotnet/maui-samples
Samples for .NET Multi-Platform App UI (.NET MAUI)
HyPlayer/HyPlayer
仅供学习交流使用 | 第三方网易云音乐播放器 | A Netease Cloud Music Player
meysamhadeli/booking-microservices
A practical microservices with the latest technologies and architectures like Vertical Slice Architecture, Event Sourcing, CQRS, DDD, gRpc, MongoDB, RabbitMq, Masstransit, and Aspire in .Net 10.
mehdihadeli/food-delivery-microservices
🍔 A practical and cloud-native food delivery microservices, built with .Net Aspire, .Net 9, MassTransit, Domain-Driven Design, CQRS, Vertical Slice Architecture, Event-Driven Architecture, and the latest technologies.
DaxStudio/DaxStudio
DAX Studio is a tool to write, execute, and analyze DAX queries in Power BI Desktop, Power Pivot for Excel, and Analysis Services Tabular.
TheCodeTraveler/GitTrends
A iOS and Android app to monitor the Views, Clones and Star history of your GitHub repos
dorisoy/Dorisoy.Pan
Dorisoy.Pan 是基于 .NET 10 的跨平台文档管理系统,使用 MS SQL 2012 / MySQL 8.0(或更高版本)后端数据库,您可以在 Windows、Linux 或 Mac 上运行它。项目中的所有方法都是异步的,支持 JWT 令牌身份验证,项目体系结构遵循 CQRS + MediatR 模式和最佳安全实践。源代码完全可定制,热插拔且清晰的体系结构,使开发定制功能和遵循任何业务需求变得容易。
BAndysc/WoWDatabaseEditor
Integrated development environment (IDE), an editor for Smart Scripts (SAI/smart_scripts) for TrinityCore based servers. Cmangos support work in progress. Featuring a 3D view built with OpenGL and custom ECS framework
meysamhadeli/booking-modular-monolith
A practical Modular Monolith architecture with the latest technologies and architecture like Vertical Slice Architecture, Event Driven Architecture, CQRS, DDD, gRpc, Masstransit, and Aspire in .Net 10.
mehdihadeli/food-delivery-modular-monolith
🌭 A practical and imaginary food and grocery delivery modular monolith, built with .Net 8, Domain-Driven Design, CQRS, Vertical Slice Architecture, Event-Driven Architecture, and the latest technologies.
nor0x/Dots
the 🙂 friendly .NET SDK manager
LuckyDucko/Mopups
Popups For MAUI
awaescher/StageManager
🖥️ Stage Manager for Microsoft Windows (feasibility study)
Goz3rr/SatisfactorySaveEditor
xamarin/dev-days-labs
mehdihadeli/vertical-slice-api-template
🍰 An asp.net core template based on .Net 9, Vertical Slice Architecture, CQRS, Minimal APIs, OpenTelemetry, API Versioning and OpenAPI.
lucacivale/Maui.BottomSheet
Native BottomSheets in .Net Maui!
stijnvdb88/Snap.Net
A cross-platform control client and player for https://github.com/badaix/snapcast
Version Downloads Last Updated
10.0.0 288,290 11/11/2025
9.0.0 857,528 11/15/2024
8.0.0 370,945 7/9/2024
7.0.0 638,709 11/14/2023
6.0.6 539,479 11/12/2022
6.0.5 277,423 7/3/2022
6.0.4 2,122,493 11/23/2021
6.0.3 9,107 11/11/2021
6.0.2 21,686 10/12/2021
6.0.1 17,432 9/27/2021
6.0.0 101,383 7/3/2021
6.0.0-pre1 1,604 6/7/2021
5.1.0 118,415 3/13/2021
5.0.2 146,848 11/2/2020
5.0.0-pre2 2,522 9/17/2020
5.0.0-pre1 1,251 9/17/2020
4.3.0 33,595 9/15/2020
4.3.0-pre1 2,540 7/29/2020
Loading failed

New In This Release:
     - Add Support for .NET 10