Soenneker.Utils.SingletonDictionary 4.0.1153

Prefix Reserved
dotnet add package Soenneker.Utils.SingletonDictionary --version 4.0.1153
                    
NuGet\Install-Package Soenneker.Utils.SingletonDictionary -Version 4.0.1153
                    
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="4.0.1153" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Utils.SingletonDictionary" Version="4.0.1153" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Utils.SingletonDictionary" />
                    
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.SingletonDictionary --version 4.0.1153
                    
#r "nuget: Soenneker.Utils.SingletonDictionary, 4.0.1153"
                    
#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@4.0.1153
                    
#: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=4.0.1153
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Utils.SingletonDictionary&version=4.0.1153
                    
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.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 AsyncLock from 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 SetInitialization before 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 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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.1153 341 1/8/2026
4.0.1152 101 1/8/2026
4.0.1151 99 1/7/2026
4.0.1150 1,383 1/6/2026
4.0.1149 290 1/6/2026
4.0.1148 290 1/6/2026
4.0.1147 5,645 1/6/2026
4.0.1146 12,753 1/5/2026
4.0.1145 1,318 1/5/2026
4.0.1144 7,526 1/3/2026
4.0.1143 528 1/3/2026
4.0.1142 7,302 1/2/2026
4.0.1141 225 1/2/2026
4.0.1140 96 1/2/2026
4.0.1139 105 1/2/2026
4.0.1138 20,302 12/31/2025
4.0.1137 1,037 12/31/2025
4.0.1136 3,466 12/31/2025
4.0.1135 13,941 12/21/2025
4.0.1134 186 12/21/2025
Loading failed