Soenneker.Utils.AsyncSingleton 4.0.830

Prefix Reserved
There is a newer version of this package available.
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
                    
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="Soenneker.Utils.AsyncSingleton" Version="4.0.830" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Utils.AsyncSingleton" Version="4.0.830" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Utils.AsyncSingleton" />
                    
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 Soenneker.Utils.AsyncSingleton --version 4.0.830
                    
#r "nuget: Soenneker.Utils.AsyncSingleton, 4.0.830"
                    
#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 Soenneker.Utils.AsyncSingleton@4.0.830
                    
#: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=Soenneker.Utils.AsyncSingleton&version=4.0.830
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Utils.AsyncSingleton&version=4.0.830
                    
Install 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

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 IDisposable or IAsyncDisposable.
  • 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
Loading failed

Updated NuGet packages