Skip to main content
net10.0.NETFramework4.6.2net8.0net9.0

Provides object pools from service containers.

Published
Oct 14, 2026
5d ago
Total Downloads
83.9K
83,893

Install

dotnet add package Microsoft.Extensions.ObjectPool.DependencyInjection --version 10.11.0
ProductVersions
.NET
net10.0net8.0net9.0
Other
.NETFramework4.6.2

README

Microsoft.Extensions.ObjectPool.DependencyInjection

This provides the ability to retrieve pooled instances that can be initialized using dependency injection.

Install the package

From the command-line:

dotnet add package Microsoft.Extensions.ObjectPool.DependencyInjection

Or directly in the C# project file:

<ItemGroup>
  <PackageReference Include="Microsoft.Extensions.ObjectPool.DependencyInjection" Version="[CURRENTVERSION]" />
</ItemGroup>

Usage Example

Registering Pools

The object pools can be registered using the following methods:

public static IServiceCollection AddPooled<TService>(this IServiceCollection services, Action<DependencyInjectionPoolOptions>? configure = null)
public static IServiceCollection AddPooled<TService, TImplementation>(this IServiceCollection services, Action<DependencyInjectionPoolOptions>? configure = null)

For example:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<MyService>();
builder.Services.AddPooled<MyPooledClass>();

var app = builder.Build();

Consuming Pools

Once registered, pools can be resolved using dependency injection. For example:

var pool = context.RequestServices.GetRequiredService<ObjectPool<MyPooledClass>>();

var obj = pool.Get();

// Use the pooled object ...

pool.Return(obj);

Pooled instances will be resolved from the root dependency injection container and can only use singleton dependencies.

Pooled instances can implement Microsoft.Extensions.ObjectPool.IResettable in order to be initialized when they are returned to the pool.

public class MyPooledClass : IResettable
{
    private MyService _myService;

    public MyPooledClass(MyService myService)
    {
        _myService = myService;
    }

    public bool TryReset()
    {
        // Clean instance here
        return true;
    }
}

Options

The DependencyInjectionPoolOptions.Capacity property is used to configure the maximum capacity of each pool. The default value is 1024.

This value can also be set during the pool registration:

builder.Services.AddPooled<MyPooledClass>(options => options.Capacity = 64);

Feedback & Contributing

We welcome feedback and contributions in our GitHub repo.

Changes from Previous Version

No structural changes between v10.10.0 and v10.11.0.

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

Compatibility metadata lists 4 target frameworks; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.