Skip to content

Dependency Injection

Mark Lauter edited this page Jun 15, 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.

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