Autofac.Pooling 2.0.0

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

Autofac.Pooling

Support for pooled instance lifetime scopes in Autofac dependency injection. Autofac can help you implement a pool of components in your application without you having to write your own pooling implementation, and making these pooled components feel more natural in the world of DI.

Build status codecov NuGet

Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.

Quick Start

Once you've added a reference to the Autofac.Pooling package, you can start using the new PooledInstancePerLifetimeScope and PooledInstancePerMatchingLifetimeScope methods:

var builder = new ContainerBuilder();

builder.RegisterType<MyCustomConnection>()
        .As<ICustomConnection>()
        .PooledInstancePerLifetimeScope();

var container = builder.Build();

using (var scope = container.BeginLifetimeScope())
{
    // Creates a new instance of MyCustomConnection
    var instance = scope.Resolve<ICustomConnection>();

    instance.DoSomething();
}

// When the scope ends, the instance of MyCustomConnection
// is returned to the pool, rather than being disposed.

using (var scope2 = container.BeginLifetimeScope())
{
    // Does **not** create a new instance, but instead gets the
    // previous instance from the pool.
    var instance = scope.Resolve<ICustomConnection>();

    instance.DoSomething();
}

// Instance gets returned back to the pool again at the
// end of the lifetime scope.

Custom Pool Providers

By default, pooled instances are stored in a DefaultObjectPool sized from the policy's MaximumRetained value. If you need full control over where instances are stored and when they are evicted (for example, a cache-backed pool with an idle timeout), you can supply your own Microsoft.Extensions.ObjectPool.ObjectPoolProvider:

var builder = new ContainerBuilder();

// Register the backing cache in Autofac. Because it is registered here, the
// container owns it and disposes it at shutdown, and it can be shared with the
// rest of the application.
builder.RegisterInstance(new MemoryCache(new MemoryCacheOptions()))
        .As<IMemoryCache>();

// Register the provider itself so Autofac constructs it and injects the cache.
builder.RegisterType<CacheObjectPoolProvider>()
        .SingleInstance();

builder.RegisterType<MyCustomConnection>()
        .As<ICustomConnection>()
        // The provider factory receives the IComponentContext, so it can resolve
        // the provider - and its dependencies - straight from the container.
        .PooledInstancePerLifetimeScope(
            ctx => ctx.Resolve<CacheObjectPoolProvider>());

var container = builder.Build();

Autofac still owns construction of the pooled instances (they are resolved through the container, so dependency injection and the IPooledComponent / IPooledRegistrationPolicy callbacks all work as normal). The provider only controls storage and eviction. The provider's Create<T>(IPooledObjectPolicy<T>) method is the seam: Autofac hands in its own policy whose Create() resolves a fully-injected instance, so your pool delegates construction to it rather than new-ing up objects itself.

A cache-backed provider that takes its cache from Autofac looks like this:

public sealed class CacheObjectPoolProvider : ObjectPoolProvider
{
    private readonly IMemoryCache _cache;

    // The cache is injected from Autofac rather than created here, so its
    // lifetime is managed by the container and shared across every pool this
    // provider creates.
    public CacheObjectPoolProvider(IMemoryCache cache)
    {
        _cache = cache;
    }

    public override ObjectPool<T> Create<T>(IPooledObjectPolicy<T> policy)
        => new CacheObjectPool<T>(_cache, policy);
}

public sealed class CacheObjectPool<T> : ObjectPool<T>
    where T : class
{
    private readonly IMemoryCache _cache;
    private readonly IPooledObjectPolicy<T> _policy;

    public CacheObjectPool(IMemoryCache cache, IPooledObjectPolicy<T> policy)
    {
        _cache = cache;
        _policy = policy;
    }

    public override T Get()
        // Ask the cache for a stored instance; build a new one through Autofac on a miss.
        => _cache.TryGetValue(typeof(T), out T? item) && item is not null
            ? item
            : _policy.Create();

    public override void Return(T obj)
    {
        // The policy decides whether the instance is fit to be retained.
        if (_policy.Return(obj))
        {
            // Dispose the instance when the cache eventually evicts it; the pool
            // owns disposal of instances the cache drops.
            var options = new MemoryCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(5) };
            options.RegisterPostEvictionCallback((_, value, _, _) => (value as IDisposable)?.Dispose());
            _cache.Set(typeof(T), obj, options);
        }
        else if (obj is IDisposable disposable)
        {
            // The pool owns disposal of instances it declines.
            disposable.Dispose();
        }
    }
}

The pool does not implement IDisposable because it does not own the cache — Autofac creates the IMemoryCache, so Autofac disposes it. The pool only takes responsibility for the pooled instances themselves, disposing them when the policy declines a return and when the cache evicts them.

Things to know about custom providers:

  • The provider factory is invoked once per registration, when the pool is built, resolved from the pool-owning (root) scope. Because the factory gets an IComponentContext, the provider can be registered like any other component and resolved from the container, letting Autofac inject its dependencies (the IMemoryCache above). Registering the provider and cache as singletons shares one provider and cache across every type that uses them.
  • With a custom provider, IPooledRegistrationPolicy.MaximumRetained does not size the pool — the provider owns sizing and eviction. You can still supply a custom policy alongside the provider with the PooledInstancePerLifetimeScope(policyFactory, providerFactory) overload to control the Get / Return behavior.
  • The pool is shared across all lifetime scopes and threads, so your pool must be thread-safe.
  • Disposal contract:
    • If the pool implements IDisposable, the container disposes it at container shutdown.
    • Because ObjectPool<T>.Return(T) returns void (no kept/dropped signal) and there is no "permanently evicted" callback, the custom pool is responsible for disposing instances it declines on Return or evicts asynchronously. Autofac cannot see those instances.
    • Instances that never entered the pool (because the policy chose not to call the pool) are disposed by normal lifetime scope disposal.

Get Help

Need help with Autofac? We have a documentation site as well as API documentation. We're ready to answer your questions on Stack Overflow or check out the discussion forum.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 was computed.  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 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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
2.0.0 123 6/17/2026
1.0.1 112,243 10/6/2020
1.0.0 707 9/28/2020