EsoxSolutions.ObjectPool 2.1.0

dotnet add package EsoxSolutions.ObjectPool --version 2.1.0
                    
NuGet\Install-Package EsoxSolutions.ObjectPool -Version 2.1.0
                    
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="EsoxSolutions.ObjectPool" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EsoxSolutions.ObjectPool" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="EsoxSolutions.ObjectPool" />
                    
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 EsoxSolutions.ObjectPool --version 2.1.0
                    
#r "nuget: EsoxSolutions.ObjectPool, 2.1.0"
                    
#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 EsoxSolutions.ObjectPool@2.1.0
                    
#: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=EsoxSolutions.ObjectPool&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=EsoxSolutions.ObjectPool&version=2.1.0
                    
Install as a Cake Tool

EsoxSolutions.ObjectPool

Overview

EsoxSolutions.ObjectPool is a high-performance, thread-safe object pool for .NET 8+ and .NET 9. It supports automatic return of objects, async operations, performance metrics, and flexible configuration. Useful for pooling expensive resources like database connections, network clients, or reusable buffers.

Features

  • Thread-safe object pooling
  • Automatic return of objects (via IDisposable)
  • Async support (GetObjectAsync, TryGetObjectAsync)
  • Queryable and dynamic pools
  • Health monitoring and status
  • Performance metrics (exportable, Prometheus format)
  • Pool configuration (max size, validation, etc.)
  • Try* methods for safe retrieval
  • Multithreaded and high-performance (Stack/HashSet)

Usage

PoolModel

A generic wrapper for pooled objects. Use Unwrap() to access the value.

var pool = new ObjectPool<int>(new List<int> { 1, 2, 3 });
using (var model = pool.GetObject())
{
    var value = model.Unwrap();
    Console.WriteLine(value);
}

ObjectPool

Administers a fixed set of objects. Throws if pool is empty.

var initialObjects = new List<int> { 1, 2, 3 };
var pool = new ObjectPool<int>(initialObjects);
using (var model = pool.GetObject())
{
    Console.WriteLine(model.Unwrap());
}
Async Usage
using (var model = await pool.GetObjectAsync())
{
    Console.WriteLine(model.Unwrap());
}
Try Methods
if (pool.TryGetObject(out var model))
{
    using (model)
    {
        Console.WriteLine(model.Unwrap());
    }
}
Pool Configuration
var config = new PoolConfiguration {
    MaxPoolSize = 5,
    MaxActiveObjects = 3,
    ValidateOnReturn = true,
    ValidationFunction = obj => obj != null
};
var pool = new ObjectPool<int>(initialObjects, config);

QueryableObjectPool

Query for objects matching a predicate.

var pool = new QueryableObjectPool<int>(new List<int> { 1, 2, 3 });
using (var model = pool.GetObject(x => x == 2))
{
    Console.WriteLine(model.Unwrap());
}

DynamicObjectPool

Creates objects on the fly using a factory method.

var pool = new DynamicObjectPool<int>(() => 42);
using (var model = pool.GetObject())
{
    Console.WriteLine(model.Unwrap());
}

Health Monitoring & Metrics

var health = pool.GetHealthStatus();
Console.WriteLine($"Healthy: {health.IsHealthy}, Warnings: {health.WarningCount}");

var metrics = pool.ExportMetrics();
foreach (var kv in metrics)
    Console.WriteLine($"{kv.Key}: {kv.Value}");


Version history:

  • 2.1.0: Added PoolConfiguration, improved health checks, fixed async disposal for the queryable pool. Improved thread-safety for all pools.
  • 2.0.0: Async support, performance metrics, Try* methods, improved performance
  • 1.1.5: Improved thread-safety, dynamic pool throws if no match
  • 1.1.3: Added DynamicObjectPool
  • 1.1.2: Improved threadsafety
  • 1.1.1: Added QueryableObjectPool

Future work

  • Timeout/disposal for idle objects
  • More advanced health checks
  • Integration with dependency injection

For bug reports or suggestions, contact info@esoxsolutions.nl


Disclaimer

Use of this software is at your own risk. The author(s) of EsoxSolutions.ObjectPool are not liable for any damages, losses, or other consequences resulting from the use, misuse, or inability to use this software.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

version 2.0: Added async support, performance metrics, Try* methods, improved performance with Stack/HashSet