Attribute reference
All attributes live in the Awaiten namespace. A property you do not set keeps the default listed here. The See column links to the page that explains the option.
Most attributes take a generic form and a typeof form. The generic form reads better and the compiler checks the type constraints. The typeof form also accepts an unbound generic such as typeof(Repository<>), which cannot be a type argument, and works below C# 11 (see Requirements).
Containers and modules
Container
Marks a static partial class as a container. The generator adds the nested Root and Scope types to it. See Getting started.
[Container]
[Container(AsyncSafety = AsyncSafety.SyncAfterInit, LifetimeSafety = LifetimeSafety.Loose)]
[Container(Extends = typeof(CoffeeShop))]
| Property | Type | Default | Meaning | See |
|---|---|---|---|---|
AsyncSafety | AsyncSafety | Strict | Whether an async-initialized service is reachable only asynchronously, or also synchronously after warm-up. | Async initialization |
LifetimeSafety | LifetimeSafety | Strict | Whether the root withholds disposable transients and a root-owned Func over one is an error. | Lifetime safety |
Extends | Type? | null | Another container whose whole composition this one starts from. The base's options apply, so a container that sets Extends does not set AsyncSafety or LifetimeSafety itself (AWT225). The base must not extend another container (AWT224). | Extending containers |
Goes on a class once. The class must be static (AWT116).
Module
Marks a static class as a reusable set of registrations that a container pulls in with [Import]. A module carries the same registration attributes as a container. It has no properties. The class must be static (AWT152). See Modules.
[Module]
Goes on a class once. A module in a library that other assemblies import is partial as well, so the generator can add the code those containers need. See Shipping modules from a library.
Import
Pulls a module's registrations into a container. The container's own registrations take precedence over imported ones. Imports are followed one level deep: a module with its own [Import] is an error (AWT150). See Modules.
[Import(typeof(PaymentModule))]
| Property | Type | Default | Meaning | See |
|---|---|---|---|---|
Module | Type | constructor argument | The module to import. | Modules |
Goes on a container, any number of times.
Registrations
Singleton, Scoped and Transient
Register an implementation with a lifetime. The three attributes share their forms and properties, except that Eager, EagerWhen and Instance exist only on [Singleton].
[Singleton<Grinder>] // the implementation is its own service
[Scoped<CardTerminal, IPaymentTerminal>] // implementation first, then the service
[Transient(typeof(Repository<>), typeof(IRepository<>))] // typeof form, here an open generic
[Singleton(typeof(Grinder))] // typeof form, its own service
| Form | Registers |
|---|---|
[Singleton<TImplementation>] | TImplementation as itself. With Factory or Instance it may be an interface or abstract type, since the container constructs nothing. |
[Singleton<TImplementation, TService>] | TImplementation under TService. TImplementation must derive from or implement TService. |
[Singleton(typeof(Impl))] | Like the first form. Accepts an unbound generic. |
[Singleton(typeof(Impl), typeof(Service))] | Like the second form. With unbound generics, each closing the graph asks for is built separately. See Open generics. |
| Property | Type | Default | Meaning | See |
|---|---|---|---|---|
Implementation | Type | constructor argument | typeof form only: the type to construct. | Open generics |
Service | Type | constructor argument, else Implementation | typeof form only: the type the instance resolves as. | Lifetimes |
Factory | string? | null | Name of a method of the container or module that produces the instance instead of a constructor. Its parameters resolve from the graph. On an open generic it names a generic method with the service's type parameters. | Factories |
Instance | string? | null | [Singleton] only: name of a field or property that holds a pre-built instance. The container neither constructs nor disposes it. Needs a closed type. | Instances |
Key | object? | null | Registers under a key, so several implementations can share a service type. A consumer selects one with [FromKey]. | Keyed services |
WhenInjectedInto | Type? | null | Uses this registration only for the constructor parameters and [Inject] properties of the given consumer type, and the unconditional registration everywhere else. | Contextual binding |
Fallback | Fallback | None | Makes the registration an overridable default that a registration of the same service without Fallback replaces, unless that is a scan match or the closing of an open generic over a closed default. | Overridable defaults |
Eager | bool | false | [Singleton] only: builds the singleton when the root is created instead of on first resolve. Needs a closed type. | Lifetimes |
EagerWhen | string? | null | [Singleton] only: name of a static bool method, property or field on the declaring type. When it is true, the singleton is built when the root is created, like Eager. It is evaluated once per root. Needs a closed type and cannot be combined with Eager. | Lifetimes |
OnActivated | string? | null | Name of a static method that receives the instance once it is constructed. A method returning Task or ValueTask makes the service async-initialized. | Lifecycle hooks |
OnRelease | string? | null | Name of a static method that receives the instance when its owning container or scope is disposed, before any instance it owns is disposed. A method returning Task or ValueTask is awaited, which needs asynchronous disposal. | Lifecycle hooks |
SuppressDisposal | bool | false | Builds the instance but never disposes it. Releasing it is up to you, typically through OnRelease. | Disposal |
ConsumerOwned | Type[]? | null | Services whose instances this consumer's Func factories and dispatch dictionaries build untracked, so the consumer disposes them. Other consumers keep container-owned instances. | Lifetime safety |
Go on a container or module, any number of times.
Decorate
Wraps every resolution of a service in a decorator. The decorator takes the inner instance in a constructor parameter of the service or of a base interface of it. When several parameters accept the service, the one whose type converts to all the others receives it (AWT124). The decorator inherits the lifetime of what it wraps. See Decorators.
[Decorate<LoggingBrewer, IBrewer>]
[Decorate<LoggingTerminal, IPaymentTerminal>(Keys = new object?[] { null, "backup" }, Order = 1)]
[Decorate(typeof(LoggingBehavior<>), typeof(IHandler<>))] // every closing of an open generic
| Property | Type | Default | Meaning | See |
|---|---|---|---|---|
Decorator | Type | constructor argument | typeof form only: the open generic decorator. | Decorators |
Service | Type | constructor argument | typeof form only: the open generic service whose closings it wraps. | Decorators |
Order | int | 0 | Position in the chain, ascending, outermost last. Ties keep declaration order, where the last declared is outermost. | Chain several |
Keys | object?[]? | null | The keys whose registrations to decorate. A null entry selects the unkeyed registrations. Unset, only the unkeyed registrations are decorated. Cannot combine with AllKeys. | Keyed registrations |
AllKeys | bool | false | Decorates the unkeyed registrations and those under every key. Cannot combine with Keys. | Keyed registrations |
When | string? | null | Name of a static bool method, property or field on the declaring type. When it is false, the decorator is never built. It is evaluated once per root. | Apply a decorator conditionally |
Goes on a container or module, any number of times.
Composite
Exposes one implementation as the single service and hands it every other registration of that service through a collection constructor parameter. See Composites.
[Composite<CompositeReceiptChannel, IReceiptChannel>]
[Composite<CompositeReceiptChannel, IReceiptChannel>(Key = "night", Lifetime = Lifetime.Singleton)]
[Composite(typeof(CompositeHandler<>), typeof(IHandler<>))] // every closing of an open generic
| Property | Type | Default | Meaning | See |
|---|---|---|---|---|
Composite | Type | constructor argument | typeof form only: the open generic composite. | Composites |
Service | Type | constructor argument | typeof form only: the open generic service it fronts. | Composites |
Lifetime | Lifetime | Transient | The composite's own lifetime. Members keep theirs. | Composites |
Key | object? | null | The key whose registrations the composite fronts. Unset, it fronts the unkeyed registrations. | Front a key |
Goes on a container or module, any number of times.
Scan
Registers every concrete class that matches, from metadata the compiler reads at build time. See Scanning.
[Scan<IDrink>] // classes assignable to the marker
[Scan(typeof(IScreen<>))] // classes implementing a closing of an open marker
[Scan(NamePatterns = new[] { "*Handler" })] // markerless: matched by the filters alone
| Property | Type | Default | Meaning | See |
|---|---|---|---|---|
AssignableTo | Type? | constructor argument | The marker interface or base type passed as typeof to the non-generic [Scan], or null for its parameterless, markerless form. | Scan without a marker |
Lifetime | Lifetime | Transient | The lifetime of every match. | Choose the lifetime |
As | ScanAs | Self | How each match registers. | Choose how they register |
InAssembliesOf | Type[]? | null | Scans the assemblies of these types instead of the container's own assembly. | Scan other assemblies |
NamePatterns | string[]? | null | Globs on the simple type name. * matches any run of characters, a ! prefix excludes. | Narrow the matches |
NamespacePatterns | string[]? | null | Globs on the namespace, by segment: * matches one segment, ** any number. A ! prefix excludes. | Narrow the matches |
Exclude | Type[]? | null | Types to leave out, by exact identity. | Narrow the matches |
SkipUnconstructable | bool | false | Skips a match the container cannot construct with the warning AWT141 instead of the error AWT101. | Leave out types that are not services |
OnActivated | string? | null | A static hook that receives each match once it is constructed. | Lifecycle hooks |
OnRelease | string? | null | A static hook that receives each match when its owner is disposed. | Lifecycle hooks |
Goes on a container or module, any number of times. A markerless scan cannot use ScanAs.Marker and needs an InAssembliesOf, or a NamePatterns or NamespacePatterns filter whose patterns each name something: a filter of wildcards only, such as "*" or "**", matches everything and does not count (AWT183).
AsyncInitializer
Declares an async initializer convention: every service of the container or module whose implementation implements the interface or derives from the class T is initialized by awaiting the named method once, before it is handed out. The method is an instance method of T that returns Task or ValueTask, is not overloaded, and has its parameters resolved from the graph. A CancellationToken parameter receives the token of the call that triggered the initialization. See Async initialization.
[AsyncInitializer<IWarmUp>(nameof(IWarmUp.WarmUpAsync))]
| Property | Type | Default | Meaning | See |
|---|---|---|---|---|
Method | string | constructor argument | The initializing method of T. | Which conventions apply |
Generic form only. Goes on a container or module, any number of times. A convention applies to the registrations of the container or module that declares it.
Host services
ImportService
Declares a service the host provides: every direct dependency of that type, keyed or not, is resolved from the container's external resolver instead of being reported as missing. A relationship or collection over it is not routed. See External services.
[ImportService<IPaymentTerminal>]
[ImportService(typeof(ILogger<>))] // every closing of a generic type
| Property | Type | Default | Meaning | See |
|---|---|---|---|---|
Service | Type | constructor argument | typeof form only: a closed type, or an unbound generic whose closings are all routed. | Pull every closing |
Goes on a container or module, any number of times. Registering the same type on the container as well is an error (AWT175).
ImportServices
Routes every unkeyed direct dependency that has no registration to the external resolver. It trades the compile-time missing-dependency check for flexibility, so prefer [ImportService] per type. It has no properties. See External services.
[ImportServices]
Goes on a container or module, once. Keyed dependencies, relationships and injected properties are not routed.
Injection sites
Inject
Marks a settable or init property of an implementation to fill from the graph after construction. The property is required unless it is Optional. See Property injection.
[Inject] public ILogger Logger { get; init; }
[Inject(Optional = true, Deferred = true)] public IAudit? Audit { get; set; }
| Property | Type | Default | Meaning | See |
|---|---|---|---|---|
Optional | bool | false | Leaves the property at its default when nothing is registered, instead of AWT101. | Optional |
Deferred | bool | false | Assigns the property after the instance is constructed and cached, which can break a constructor cycle. Needs a plain set accessor. | Deferred |
Goes on a property, once. Combine with [FromKey] to select a keyed registration.
InjectProperty
The container-side counterpart of [Inject]: names a property of an implementation to fill, so the implementation stays free of Awaiten attributes. It applies wherever the implementation is constructed, scans included. See Property injection.
[InjectProperty<OrderService>(nameof(OrderService.Logger))]
[InjectProperty(typeof(Repository<>), nameof(Repository<object>.Logger))] // every closing
| Property | Type | Default | Meaning | See |
|---|---|---|---|---|
Implementation | Type | constructor argument | typeof form only: a closed type or an unbound generic. | Open generics |
PropertyName | string | constructor argument | The property to fill. Use nameof. | Property injection |
Optional | bool | false | As on [Inject]. | Optional |
Deferred | bool | false | As on [Inject]. | Deferred |
Key | object? | null | Selects the keyed registration of the property's type, like [FromKey]. | Key |
Goes on a container, once per property.
FromKey
Selects the keyed registration for a constructor parameter, factory or hook parameter, or injected property. See Keyed services.
public sealed class Latte([FromKey("Oat")] IMilk milk);
public sealed class Checkout([FromKey(MilkKind.Oat)] IMilk milk); // an enum constant as the key
| Property | Type | Default | Meaning | See |
|---|---|---|---|---|
Key | object | constructor argument | The key to select: a string, an enum value or a typeof(...). | Typed keys |
Goes on a parameter or property, once.
Arg
Marks a parameter of a Factory method or of a constructor as a runtime argument. The factory form keeps the produced type free of Awaiten. A service with [Arg] parameters is built fresh on every call and resolves only through a Func whose leading type arguments supply the marked parameters in order: Func<TArg…, T>, Func<TArg…, Owned<T>>, Func<TArg…, Task<T>> or Func<TArg…, Task<Owned<T>>>. It has no properties. See Runtime arguments.
private static Cup MakeCup(EspressoMachine machine, [Arg] string size) => new(machine, size); // factory form
public sealed class Cup(EspressoMachine machine, [Arg] string size); // constructor form
Goes on a parameter, once.
RequestingType
Marks a Type? parameter of a Factory method that receives the type of the consumer the service is built for, such as the class that uses a logger. It is null when the service is resolved straight from the container. It has no properties. See Context-aware factories.
private static ILogger CreateLogger([RequestingType] Type? consumer) => /* … */;
Goes on a parameter, once. A parameter that is not Type is AWT162.
Inherited
Marks a parameter of a Factory method on a container with Extends that takes the registration of the base which the method's registration replaces, so a test container can fall back to production. Take the service as itself, Lazy<T>, Func<T> or their async forms. It has no properties. See Extending containers.
private static IPaymentTerminal Terminal(TestDoubles doubles, [Inherited] Func<IPaymentTerminal> production)
=> doubles.Terminal ?? production();
Goes on a parameter, once. Anywhere else, or together with [Arg] or [FromKey], it is AWT264.
Enums
Lifetime
| Value | Meaning |
|---|---|
Singleton (0) | One instance for the life of the container. |
Scoped (1) | One instance per scope. The root acts as the outermost scope. |
Transient (2) | A new instance for every resolve or injection. |
Used by [Composite] and [Scan], and reported in AwaitenRegistration.Lifetime. The values match ServiceLifetime of Microsoft.Extensions.DependencyInjection. See Lifetimes.
AsyncSafety
| Value | Meaning |
|---|---|
Strict (0, default) | An async-initialized service is reachable only asynchronously, through ResolveAsync or an awaiting shape such as Task<T>. A synchronous shape over one is a compile-time error, and a synchronous resolve throws. |
SyncAfterInit (1) | An async-initialized service also resolves synchronously. After InitializeAsync warmed it, that returns at once; before, it blocks until the initialization completes. |
See Async initialization.
LifetimeSafety
| Value | Meaning |
|---|---|
Strict (0, default) | A disposable transient is withheld from by-type resolution on the root, and a root-owned Func over one is the error AWT118. Constructor injection, child scopes and Owned<T> still reach it. |
Loose (1) | Everything resolves everywhere, as in Microsoft.Extensions.DependencyInjection. AWT118 becomes a warning. |
See Lifetime safety.
Fallback
| Value | Meaning |
|---|---|
None (0, default) | A normal registration. |
Warn (1) | An overridable default. When another Warn default provides the same service and nothing stronger decides, AWT148 warns and the first declared wins. |
Silent (2) | An overridable default that defers without a warning. |
See Overridable defaults.
ScanAs
A flags enum: combine values with the | operator, such as ScanAs.Marker | ScanAs.Self.
| Value | Meaning |
|---|---|
Self (1, default) | Registers each match as its own type. |
Marker (2) | Registers each match under the scanned marker, which makes the matches a collection of the marker. |
MatchingInterface (4) | Registers each match under the interface named I plus its own name, such as IOrderService for OrderService. |
Where to go next
- Runtime API for what you call on a container.
- Diagnostics for every check the attributes are subject to.