![]() |
VOOZH | about |
dotnet add package Soenneker.Utils.AsyncSingleton --version 4.0.797
NuGet\Install-Package Soenneker.Utils.AsyncSingleton -Version 4.0.797
<PackageReference Include="Soenneker.Utils.AsyncSingleton" Version="4.0.797" />
<PackageVersion Include="Soenneker.Utils.AsyncSingleton" Version="4.0.797" />Directory.Packages.props
<PackageReference Include="Soenneker.Utils.AsyncSingleton" />Project file
paket add Soenneker.Utils.AsyncSingleton --version 4.0.797
#r "nuget: Soenneker.Utils.AsyncSingleton, 4.0.797"
#:package Soenneker.Utils.AsyncSingleton@4.0.797
#addin nuget:?package=Soenneker.Utils.AsyncSingleton&version=4.0.797Install as a Cake Addin
#tool nuget:?package=Soenneker.Utils.AsyncSingleton&version=4.0.797Install as a Cake Tool
👁 alternate text is missing from this package README image
👁 alternate text is missing from this package README image
👁 alternate text is missing from this package README image
👁 alternate text is missing from this package README image
AsyncSingleton is a lightweight utility that provides lazy (and optionally asynchronous) initialization of an instance. It ensures that the instance is only created once, even in highly concurrent scenarios. It also offers both synchronous and asynchronous initialization methods while supporting a variety of initialization signatures. Additionally, AsyncSingleton implements both synchronous and asynchronous disposal.
Get(), GetAsync(), Init() or InitSync().params object[])CancellationTokendotnet add package Soenneker.Utils.AsyncSingleton
There are two different types: AsyncSingleton, and AsyncSingleton<T>:
AsyncSingleton<T>Useful in scenarios where you need a result of the initialization. Get() is the primary method.
using Microsoft.Extensions.Logging;
public class MyService
{
private readonly ILogger<MyService> _logger;
private readonly AsyncSingleton<HttpClient> _asyncSingleton;
public MyService(ILogger<MyService> logger)
{
_logger = logger;
_asyncSingleton = new AsyncSingleton(async () =>
{
_logger.LogInformation("Initializing the singleton resource synchronously...");
await Task.Delay(1000);
return new HttpClient();
});
}
public async ValueTask StartWork()
{
var httpClient = await _asyncSingleton.Get();
// At this point the task has been run, guaranteed only once (no matter if this is called concurrently)
var sameHttpClient = await _asyncSingleton.Get(); // This is the same instance of the httpClient above
}
}
AsyncSingletonUseful in scenarios where you just need async single initialization, and you don't ever need to leverage an instance. Init() is the primary method.
using Microsoft.Extensions.Logging;
public class MyService
{
private readonly ILogger<MyService> _logger;
private readonly AsyncSingleton _singleExecution;
public MyService(ILogger<MyService> logger)
{
_logger = logger;
_singleExecution = new AsyncSingleton(async () =>
{
_logger.LogInformation("Initializing the singleton resource ...");
await Task.Delay(1000); // Simulates an async call
return new object(); // This object is needed for AsyncSingleton to recognize that initialization has occurred
});
}
public async ValueTask StartWork()
{
await _singleExecution.Init();
// At this point the task has been run, guaranteed only once (no matter if this is called concurrently)
await _singleExecution.Init(); // This will NOT execute the task, since it's already been called
}
}
Tips:
CancellationToken to the Init(), and Get() method. This will cancel any locking occurring during initialization.AsyncSingleton that implements IDisposable or IAsyncDisposable, be sure to dispose of the AsyncSingleton instance. This will dispose the underlying instance.AsyncSingleton holds a reference to it, and will return those changes to further callers.SetInitialization() can be used to set the initialization function after the AsyncSingleton has been created. This can be useful in scenarios where the initialization function is not known at the time of creation.AsyncSingleton will block to maintain thread-safety.IAsyncDisposable, try to leverage AsyncSingleton.DisposeAsync(). Using AsyncSingleton.DisposeAsync() with an IDisposable underlying instance is fine.| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
Showing the top 5 NuGet packages that depend on Soenneker.Utils.AsyncSingleton:
| Package | Downloads |
|---|---|
|
Soenneker.Utils.MemoryStream
An easy modern MemoryStream utility |
|
|
Soenneker.Utils.Runtime
A collection of helpful runtime-based operations |
|
|
Soenneker.Redis.Client
A utility library for Redis client accessibility |
|
|
Soenneker.GitHub.Client
An async thread-safe singleton for Octokit's GitHubClient |
|
|
Soenneker.ServiceBus.Admin
A utility library for Azure Service Bus Administration client accessibility |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.0.797 | 10,997 | 6/16/2026 |
| 4.0.796 | 97,554 | 6/6/2026 |
| 4.0.795 | 16,369 | 6/6/2026 |
| 4.0.794 | 370 | 6/6/2026 |
| 4.0.792 | 7,044 | 6/5/2026 |
| 4.0.791 | 382 | 6/5/2026 |
| 4.0.789 | 6,381 | 6/5/2026 |
| 4.0.788 | 209,876 | 4/23/2026 |
| 4.0.787 | 3,789 | 4/23/2026 |
| 4.0.786 | 9,719 | 4/23/2026 |
| 4.0.785 | 18,608 | 4/22/2026 |
| 4.0.784 | 9,024 | 4/22/2026 |
| 4.0.783 | 176,864 | 3/13/2026 |
| 4.0.782 | 3,804 | 3/13/2026 |
| 4.0.781 | 5,740 | 3/13/2026 |
| 4.0.780 | 2,291 | 3/12/2026 |
| 4.0.779 | 215 | 3/12/2026 |
| 4.0.778 | 1,982 | 3/12/2026 |
| 4.0.776 | 566 | 3/12/2026 |
| 4.0.773 | 2,779 | 3/12/2026 |
Update dependency Soenneker.Asyncs.Locks to 4.0.53 (#996)