Soenneker.Utils.SingletonDictionary
3.0.1103
Prefix Reserved
dotnet add package Soenneker.Utils.SingletonDictionary --version 3.0.1103
NuGet\Install-Package Soenneker.Utils.SingletonDictionary -Version 3.0.1103
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.SingletonDictionary" Version="3.0.1103" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Utils.SingletonDictionary" Version="3.0.1103" />
<PackageReference Include="Soenneker.Utils.SingletonDictionary" />
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.SingletonDictionary --version 3.0.1103
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Soenneker.Utils.SingletonDictionary, 3.0.1103"
#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.SingletonDictionary@3.0.1103
#: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.SingletonDictionary&version=3.0.1103
#tool nuget:?package=Soenneker.Utils.SingletonDictionary&version=3.0.1103
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Soenneker.Utils.SingletonDictionary
A flexible singleton dictionary with double-check async locking, sync/async disposal, and external initialization
Features
- ✅ Async and sync initialization patterns
- ✅ Optional cancellation support
- ✅ Async and sync access methods
- ✅ Fully disposable (sync and async)
- ✅ Thread-safe with
AsyncLockfrom Nito.AsyncEx
Installation
dotnet add package Soenneker.Utils.SingletonDictionary
✨ Example Usage
Here’s an example using SingletonDictionary to manage singleton HttpClient instances keyed by configuration (e.g. timeout):
public class HttpRequester : IDisposable, IAsyncDisposable
{
private readonly SingletonDictionary<HttpClient> _clients;
public HttpRequester()
{
_clients = new SingletonDictionary<HttpClient>((args) =>
{
var socketsHandler = new SocketsHttpHandler
{
PooledConnectionLifetime = TimeSpan.FromMinutes(10),
MaxConnectionsPerServer = 10
};
var client = new HttpClient(socketsHandler)
{
Timeout = TimeSpan.FromSeconds((int)args[0])
};
return client;
});
}
public async ValueTask Get()
{
var client = await _clients.Get("100", 100);
await client.GetAsync("https://google.com");
}
public async ValueTask DisposeAsync()
{
GC.SuppressFinalize(this);
await _clients.DisposeAsync();
}
public void Dispose()
{
GC.SuppressFinalize(this);
_clients.Dispose();
}
}
🔍 Internals
SingletonDictionary<T> is backed by a ConcurrentDictionary<string, T> and supports:
- Multiple constructor overloads for async/sync factory functions
- Internal double-checked locking on access
- Deferred/lazy factory execution
- Proper disposal of values (both sync and async interfaces)
- Safe mutation via
SetInitializationbefore first use
Example constructor overloads include:
new SingletonDictionary<T>(Func<string, object[], ValueTask<T>> factory);
new SingletonDictionary<T>(Func<object[], T> factory);
new SingletonDictionary<T>(Func<string, CancellationToken, object[], ValueTask<T>> factory);
// And more...
You can also initialize manually:
var dict = new SingletonDictionary<MyService>();
dict.SetInitialization((args) => new MyService(args));
🛡️ Thread Safety
This library uses AsyncLock for safe concurrent access in async contexts, and synchronously via Lock() for blocking methods. This avoids race conditions and guarantees safe singleton creation.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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.
-
net9.0
- Intellenum (>= 2.0.0)
- Nito.AsyncEx (>= 5.1.2)
- Soenneker.Extensions.Enumerable (>= 3.0.562)
- Soenneker.Extensions.ValueTask (>= 3.0.94)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.