Skip to main content

Introduction

Awaiten is a compile-time dependency injection container for .NET. You declare your services with attributes on a container class, and a Roslyn source generator writes plain C# that builds the object graph. Nothing is discovered, reflected over or emitted at run time, so the result is trimming and native AOT clean, and you can read and step through it like your own code.

Awaiten treats asynchronous initialization as part of the graph. A service that needs async setup before it can work, such as an espresso machine that heats its boiler or a payment terminal that connects to the bank, is tracked through the whole graph, including relationships, collections and scopes. The compiler stops any code path that could hand it out before its setup has finished.

What the compiler checks for you​

The generator checks the whole graph while you build and reports each mistake with an AWT id, as a build error or a warning. There are more than 100 of them, among others for:

  • a dependency that is not registered,
  • a dependency cycle,
  • a singleton that captures a scoped service,
  • an async-initialized service reached through a synchronous path,
  • a Func on the container root that builds disposable services, whose instances would pile up there for the root's lifetime.

The examples on this page assume using Awaiten; and coffee-shop types such as IPaymentTerminal and CardTerminal declared elsewhere.

Forget to register the payment terminal the barista needs, and the build stops instead of the first order:

public sealed class Barista(IPaymentTerminal terminal);

[Container]
[Scoped<Barista>] // nothing provides an IPaymentTerminal
public static partial class CoffeeShop;
Build error
error AWT101: 'Barista' cannot be resolved: 'Barista' requires 'IPaymentTerminal', which is not registered on the container

Register it, and the build passes:

public sealed class Barista(IPaymentTerminal terminal);

[Container]
[Singleton<CardTerminal, IPaymentTerminal>]
[Scoped<Barista>]
public static partial class CoffeeShop;

The Diagnostics page lists every check with an example and a fix.

How it compares​

The columns for other containers describe each category in general. Check a library's own documentation for its specifics.

AwaitenMicrosoft.Extensions.DependencyInjectionReflection-based containers such as Autofac, DryIoc or Simple InjectorOther source-generated containers such as Jab, StrongInject or Pure.DI
When the graph is checkedAt build time, by the compilerAt run time, optionally when the provider is builtAt run time, some with a verification call at startupAt build time
Reflection at run timeNoneUsed to activate servicesUsed to discover and activate servicesNone or little
Async initializationTracked through the graph and checked at build timeNot built inNot checked at build timeVaries by library
Registration styleAttributes on a container classCalls on an IServiceCollection at run timeRegistration code at run timeAttributes or setup code that the generator reads
Registrations decided by run-time codeNot possiblePossiblePossibleNot possible
Native AOT and trimmingSupportedSupported, with limitsDepends on the libraryGenerally supported

For resolve and build times against Microsoft's container, Autofac, DryIoc and Simple Injector, see the Benchmarks.

When to pick another container​

Awaiten fixes the graph at build time. That is its strength, and it rules out designs that decide the graph at run time. Another container fits better when you need:

  • Registrations that change at run time, such as replacing or removing a registration, or registering a service only when a setting is on. In Awaiten, a factory that takes the setting as a parameter makes such a choice.
  • Plugins loaded at run time, where assemblies are discovered and their types activated by reflection. Awaiten's scanning only sees what the compilation references.
  • Lambda factories that capture state. A factory in Awaiten is a named static method on the container or a module, and the values it needs arrive as parameters.
  • Last registration wins. Awaiten resolves the first registration, and an override is declared explicitly.
  • A wildcard key such as KeyedService.AnyKey. A key in Awaiten selects exactly one registration.

Migrating from MS.DI lists these differences with the Awaiten way for each. You do not have to move everything at once: the MS.DI bridge runs a container next to an IServiceCollection, so what has no counterpart can stay in MS.DI.

Frameworks and requirements​

  • Your project targets anything from .NET Framework 4.8 and netstandard2.0 up to .NET 10. The generated code compiles for each of them.
  • You build with the .NET SDK 10.0.300 or later and C# 11 or later, or C# 9 with the typeof forms as long as no module declares a [Scan].
  • Awaiten is the only package you need. Add Awaiten.Extensions.DependencyInjection to host a container in ASP.NET Core or the generic host.

Getting started has the details, including older C# versions and IDE versions.

Where to go next​