Skip to main content

Lifecycle hooks

Like a factory, a lifecycle hook is a method on the container. It lets you run code when an instance is activated and when it is released, without putting that code in the service itself. Calibrate the espresso machine when it comes online. Purge it when the shop closes.

OnActivated and OnRelease​

Name a hook method with OnActivated or OnRelease. The method lives on the container and takes the instance. Declare the parameter as the implementation type and Awaiten hands it over directly, no cast needed.

[Container]
[Singleton<EspressoMachine>(OnActivated = nameof(Calibrate), OnRelease = nameof(Purge))]
public static partial class CoffeeShop
{
private static void Calibrate(EspressoMachine machine) => machine.Calibrate();
private static void Purge(EspressoMachine machine) => machine.Purge();
}

You can type the parameter as object instead when one hook serves several implementations.

OnActivated runs once, right after construction. For an async service it runs before the async initializer conventions. OnRelease runs when the owner or scope is disposed, in reverse creation order, and every release hook of the owner or scope runs before any of its instances is disposed. By default it runs in addition to that disposal; pair it with SuppressDisposal to replace disposal entirely, as when a hook returns an object to a pool.

Hook parameters​

Both hooks may declare parameters after the instance. The first parameter is always the instance; every parameter after it is resolved from the container graph, exactly like a constructor or factory parameter. Calibrate the machine from injected settings; return a pooled buffer to the pool it came from.

[Container]
[Singleton<Settings>]
[Singleton<BufferPool>]
[Singleton<EspressoMachine>(OnActivated = nameof(Calibrate))]
[Transient<Buffer>(Factory = nameof(Rent), OnRelease = nameof(ReturnToPool))]
public static partial class CoffeeShop
{
private static void Calibrate(EspressoMachine machine, Settings settings) => machine.Calibrate(settings);
private static Buffer Rent(BufferPool pool) => pool.Rent();
private static void ReturnToPool(Buffer buffer, BufferPool pool) => pool.Return(buffer);
}

An activation dependency is resolved inline as the hook is called. A release dependency is resolved at construction and captured by value into the queued closure, so the hook holds the instance it was queued for even if a later resolve fails and rolls back. A captured dependency (a singleton pool, say) is still alive when the hook runs, because the release hooks all run before any disposal starts.

Hook parameters participate in the graph like any other dependency, including cycle, captive-dependency and async-taint analysis. The rules:

  • An unregistered hook parameter is a missing dependency (AWT101).
  • A hook parameter cannot be a runtime [Arg], because no Func<…> call supplies one (AWT189).
  • A release hook parameter cannot be a Func or Lazy relationship, synchronous or async, or a dispatch dictionary (AWT191). Those resolve when called, and by the time the release hook runs the owner is already tearing down.
  • An activation hook may take a Func<T> or Lazy<T>, but calling one over the singleton or scoped service it activates throws while that service is not cached yet, see Relationships.

A C# default value does not make a hook parameter optional (AWT101, see Prefer constructor injection). To let the hook run without the service, take a collection of it, which is empty when nothing registers the service. A relationship such as Func<T> or Lazy<T> does not help, because it needs the service registered too. When the service itself uses the dependency, give the service an Optional injected property instead of passing the dependency through the hook.

[Container]
[Singleton<EspressoMachine>(OnActivated = nameof(Calibrate))]
public static partial class CoffeeShop
{
private static void Calibrate(EspressoMachine machine, IEnumerable<Settings> settings)
{
if (settings.FirstOrDefault() is { } shopSettings)
{
machine.Calibrate(shopSettings);
}
}
}

Asynchronous activation​

An OnActivated hook may return Task or ValueTask. The container awaits it, so the service becomes async-initialized, like a service an async initializer convention matches. Connect the payment terminal without putting Awaiten on it.

[Container]
[Singleton<Settings>]
[Singleton<PaymentTerminal, IPaymentTerminal>(OnActivated = nameof(ConnectAsync))]
public static partial class CoffeeShop
{
private static Task ConnectAsync(PaymentTerminal terminal, Settings settings, CancellationToken cancellationToken)
=> terminal.HandshakeAsync(cancellationToken);
}

A CancellationToken parameter receives the token passed to ResolveAsync, InitializeAsync or CreateScopeAsync. Every other parameter after the instance resolves from the graph, as for a synchronous hook.

The service and everything that depends on it now resolve through ResolveAsync. All async rules apply: a synchronous Func<T> or Lazy<T> over it is AWT119, an Eager singleton needs AsyncSafety.SyncAfterInit (AWT161), and the MS.DI bridge exposes it as Task<T>.

The hook works on every registration the container owns: any lifetime, keyed, Factory, open generic, scan and module registrations.

Only Task and ValueTask are awaited. A hook that returns Task<T> or ValueTask<T> is AWT232, because the container has no use for the result.

Asynchronous release​

An OnRelease hook may return Task or ValueTask too. Use it for cleanup that belongs to the container, not to the service, such as flushing a shared sink or returning an object to a pool. The service itself stays free of Awaiten.

[Container]
[Singleton<ReceiptPrinter>]
[Scoped<Order>(OnRelease = nameof(PrintReceiptAsync))]
public static partial class CoffeeShop
{
private static Task PrintReceiptAsync(Order order, ReceiptPrinter printer)
=> printer.PrintAsync(order);
}

DisposeAsync awaits the hook. It keeps the place a synchronous hook has: in reverse creation order, before any instance is disposed. Release dependencies are captured at construction, as for a synchronous hook. DisposeAsync has no CancellationToken, so the hook cannot take one (AWT232).

The hook does not make the service async-initialized. It still resolves through Resolve, because the hook only runs at teardown.

Dispose the owner with await using or DisposeAsync. A synchronous Dispose cannot await the hook, so it skips it and throws once the rest is torn down, as described in Disposal (AWT156). The same holds for an Owned<T> handle and for a scope of the MS.DI bridge.

Ordering and failure​

Activation happens before the instance is ever handed out, so a concurrent resolve never sees a service that has not been activated. The hook runs first, synchronous or asynchronous. Then the container awaits each async initializer convention the service matches, in declaration order. If OnActivated or a convention throws or its task faults, the instance is not published, and a disposable instance is disposed. A later resolve builds a fresh one and tries again. Release is only queued once activation succeeds.

If OnRelease throws or its task faults, the teardown goes on: the other release hooks still run, and every instance is still disposed, including the one whose hook failed. The failure surfaces once the teardown is complete, together with any other teardown failure. See When teardown fails.

Hooks on scans and modules​

A [Scan] can name the same OnActivated/OnRelease hooks, applied to every match, so a whole scanned family shares one routine. See Scanning.

A hook of a module registration or scan may stay internal. When a container in another assembly imports the module, the library module rules apply.

Eager singletons​

Eager = true builds a singleton when the root is created. It is a lifetime option, so it is described under Lifetimes.

Rules​

  • Hooks are for services the container owns. Setting one on a pre-built Instance registration is an error (AWT165).
  • Registrations of the same implementation share one instance, so they must agree on its hooks and on Eager. Two registrations that name different hooks, or one with Eager and one explicitly without, are an error (AWT166), and so is a scan that names a hook for a match whose registration names another or none. A registration that leaves a hook unset takes the other's.

Where to go next​