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 587 1/8/2026
4.0.1152 108 1/8/2026
4.0.1151 105 1/7/2026
4.0.1150 1,388 1/6/2026
4.0.1149 295 1/6/2026
4.0.1148 295 1/6/2026
4.0.1147 5,652 1/6/2026
4.0.1146 12,764 1/5/2026
4.0.1145 1,325 1/5/2026
4.0.1144 7,572 1/3/2026
4.0.1143 537 1/3/2026
4.0.1142 7,311 1/2/2026
4.0.1141 233 1/2/2026
4.0.1140 104 1/2/2026
4.0.1139 112 1/2/2026
4.0.1138 20,311 12/31/2025
4.0.1137 1,048 12/31/2025
4.0.1136 3,473 12/31/2025
4.0.1135 13,973 12/21/2025
4.0.1134 191 12/21/2025
Loading failed