Soenneker.Utils.AsyncSingleton
4.0.830
Prefix Reserved
See the version list below for details.
dotnet add package Soenneker.Utils.AsyncSingleton --version 4.0.830
NuGet\Install-Package Soenneker.Utils.AsyncSingleton -Version 4.0.830
<PackageReference Include="Soenneker.Utils.AsyncSingleton" Version="4.0.830" />
<PackageVersion Include="Soenneker.Utils.AsyncSingleton" Version="4.0.830" />
<PackageReference Include="Soenneker.Utils.AsyncSingleton" />
paket add Soenneker.Utils.AsyncSingleton --version 4.0.830
#r "nuget: Soenneker.Utils.AsyncSingleton, 4.0.830"
#:package Soenneker.Utils.AsyncSingleton@4.0.830
#addin nuget:?package=Soenneker.Utils.AsyncSingleton&version=4.0.830
#tool nuget:?package=Soenneker.Utils.AsyncSingleton&version=4.0.830
Soenneker.Utils.AsyncSingleton
Thread-safe, lazy initialization for a single shared value, with synchronous and asynchronous factories and retrieval.
Installation
dotnet add package Soenneker.Utils.AsyncSingleton
Usage
Create the singleton with a factory, then retain and dispose the wrapper for as long as the shared value should live:
await using var client = new AsyncSingleton<HttpClient>(
static () => new ValueTask<HttpClient>(new HttpClient()));
HttpClient first = await client.Get();
HttpClient sameInstance = await client.Get();
Concurrent callers share one successful factory result. If the factory throws or is cancelled, no value is cached and a later call can retry.
Use AsyncSingleton<T, T1> when initialization needs an argument:
var client = new AsyncSingleton<HttpClient, Uri>(
static baseAddress => new HttpClient { BaseAddress = baseAddress });
HttpClient value = await client.Get(new Uri("https://api.example.com"));
Only the argument supplied by the call that successfully initializes the value is used. Arguments from later calls are ignored because those calls receive the cached instance.
Synchronous access
GetSync() is available when the caller must initialize synchronously. Prefer matching asynchronous factories with Get(); calling GetSync() for an asynchronous factory blocks until that factory completes.
Lifetime and cancellation
- Disposing the wrapper also disposes the cached value when it implements
IDisposableorIAsyncDisposable. - After disposal, retrieval throws
ObjectDisposedException. - A cancellation token can cancel lock acquisition and is passed to factories that accept a token. It cannot cancel a factory overload that has no token parameter.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Soenneker.Asyncs.Locks (>= 4.0.74)
- Soenneker.Atomics.ValueBools (>= 4.0.38)
- Soenneker.Extensions.ValueTask (>= 4.0.120)
NuGet packages (103)
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
Detects the current operating system, common hosting environments, and container execution. |
|
|
Soenneker.ServiceBus.Admin
A utility library for Azure Service Bus Administration client accessibility |
|
|
Soenneker.ServiceBus.Client
A utility library for Azure Service Bus client accessibility |
|
|
Soenneker.GitHub.ClientUtil
Provides lazy, cached access to the generated GitHub REST API client. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.0.831 | 0 | 9/3/2026 |
| 4.0.830 | 14,604 | 8/31/2026 |
| 4.0.829 | 22,684 | 8/30/2026 |
| 4.0.828 | 4,270 | 8/30/2026 |
| 4.0.826 | 11,428 | 8/30/2026 |
| 4.0.825 | 9,292 | 8/30/2026 |
| 4.0.824 | 4,057 | 8/30/2026 |
| 4.0.823 | 76 | 8/30/2026 |
| 4.0.822 | 311 | 8/29/2026 |
| 4.0.821 | 10,388 | 8/29/2026 |
| 4.0.820 | 68,586 | 8/26/2026 |
| 4.0.819 | 153,082 | 8/8/2026 |
| 4.0.818 | 21,069 | 8/8/2026 |
| 4.0.817 | 82,969 | 7/29/2026 |
| 4.0.816 | 18,982 | 7/28/2026 |
| 4.0.815 | 6,766 | 7/28/2026 |
| 4.0.814 | 7,395 | 7/28/2026 |
| 4.0.813 | 6,747 | 7/27/2026 |
| 4.0.812 | 186 | 7/27/2026 |
| 4.0.811 | 85,020 | 7/19/2026 |
Updated NuGet packages