Skip to content

Dependency Injection

Mark Lauter edited this page Jun 25, 2026 · 2 revisions

Dependency injection

Every registration extension lives in the Pool namespace. The pool, its factory, its preparation strategy, and its metrics register as singletons. This page lists each extension and what it adds; the guides cover when to reach for each.

Single pool

AddPool

IServiceCollection AddPool<TPoolItem>(
    this IServiceCollection services,
    IConfiguration configuration,
    Action<PoolOptions>? configureOptions = null)
    where TPoolItem : class;

Registers IPool<TPoolItem>, its validated PoolOptions, and the default metrics. Binds PoolOptions from the "PoolOptions" configuration section, then applies configureOptions. Also registers the default factory and preparation strategy when UseDefaultFactory and UseDefaultPreparationStrategy are set on the bound options. The options pipeline validates data annotations and runs ValidateOnStart, so an invalid value fails at host startup.

AddPoolItemFactory

IServiceCollection AddPoolItemFactory<TPoolItem, TItemFactory>(this IServiceCollection services)
    where TPoolItem : class
    where TItemFactory : class, IItemFactory<TPoolItem>;

Registers a custom item factory as a singleton. Call it before AddPool; it takes the place of the default factory.

AddPreparationStrategy

IServiceCollection AddPreparationStrategy<TPoolItem, TPreparationStrategy>(this IServiceCollection services)
    where TPoolItem : class
    where TPreparationStrategy : class, IPreparationStrategy<TPoolItem>;

Registers a custom preparation strategy as a singleton. Call it before AddPool; it takes the place of the default strategy.

AddDefaultPoolMetrics

IServiceCollection AddDefaultPoolMetrics<TPoolItem>(this IServiceCollection services)
    where TPoolItem : class;

Registers IPoolMetrics with the default implementation. AddPool calls this for you; use it directly only when you construct a pool without AddPool. See Metrics.

Named pools

AddPoolFactory

IServiceCollection AddPoolFactory<TPoolItem>(this IServiceCollection services)
    where TPoolItem : class;

Registers IPoolFactory<TPoolItem>, which resolves named pools by service key. AddNamedPool calls this for you.

AddNamedPool

IServiceCollection AddNamedPool<TPoolItem>(
    this IServiceCollection services,
    string name,
    IConfiguration configuration,
    Action<PoolOptions>? configureOptions = null)
    where TPoolItem : class;

Registers a named IPool<TPoolItem>, keyed by ServiceKey.Create<TPoolItem>(name). Binds the shared "PoolOptions" section, then the per-pool "{key}_PoolOptions" section, then applies configureOptions. See Named pools.

AddPool (typed client)

IServiceCollection AddPool<TPoolItem, TClient>(
    this IServiceCollection services,
    IConfiguration configuration,
    Action<PoolOptions>? configureOptions = null,
    Action<TClient>? configureClient = null)
    where TPoolItem : class
    where TClient : class;

Registers a named pool keyed by the client type's full name and a TClient with that pool injected into its constructor. configureClient runs against the resolved client. Inject TClient directly to use its dedicated pool.

Unbounded pool

These extensions register an unbounded pool behind the same IPool<TPoolItem> surface. They parallel the bounded Add* methods but bind UnboundedPoolOptions and back the registration with UnboundedPool<TPoolItem>.

AddUnboundedPool

IServiceCollection AddUnboundedPool<TPoolItem>(
    this IServiceCollection services,
    IConfiguration configuration,
    Action<UnboundedPoolOptions>? configureOptions = null)
    where TPoolItem : class;

Registers IPool<TPoolItem>, its validated UnboundedPoolOptions, and the default metrics. Binds UnboundedPoolOptions from the "UnboundedPoolOptions" configuration section, then applies configureOptions. Registers the default factory and preparation strategy when UseDefaultFactory and UseDefaultPreparationStrategy are set. The counterpart to AddPool.

AddNamedUnboundedPool

IServiceCollection AddNamedUnboundedPool<TPoolItem>(
    this IServiceCollection services,
    string name,
    IConfiguration configuration,
    Action<UnboundedPoolOptions>? configureOptions = null)
    where TPoolItem : class;

Registers a named unbounded pool, keyed by ServiceKey.Create<TPoolItem>(name) and resolved through IPoolFactory<TPoolItem>. Binds the shared "UnboundedPoolOptions" section, then the per-pool "{key}_UnboundedPoolOptions" section, then applies configureOptions. The counterpart to AddNamedPool.

AddUnboundedPool (typed client)

IServiceCollection AddUnboundedPool<TPoolItem, TClient>(
    this IServiceCollection services,
    IConfiguration configuration,
    Action<UnboundedPoolOptions>? configureOptions = null,
    Action<TClient>? configureClient = null)
    where TPoolItem : class
    where TClient : class;

Registers a named unbounded pool keyed by the client type's full name and a TClient with that pool injected into its constructor. configureClient runs against the resolved client. The counterpart to AddPool<TPoolItem, TClient>.

AddDefaultUnboundedPoolMetrics

IServiceCollection AddDefaultUnboundedPoolMetrics<TPoolItem>(this IServiceCollection services)
    where TPoolItem : class;

Registers IPoolMetrics named for the unbounded pool. AddUnboundedPool calls this for you; use it directly only when you construct an UnboundedPool<TPoolItem> without the registration extension. See Metrics.

Service keys

string ServiceKey.Create<TPoolItem>(string name) where TPoolItem : class;

Builds the key a named pool is registered under, of the form "{name}.{typeof(TPoolItem).Name}.Pool". Use it to resolve a pool through IPoolFactory<TPoolItem>.CreatePool rather than formatting the string by hand.

Clone this wiki locally