Troubleshooting
Each entry names a symptom, its cause and the fix, and frequently asked questions close the page. Most problems Awaiten reports at build time already; the Diagnostics page explains every AWT id.
Build
The generator does not run, or Root is not found
Symptom: CoffeeShop.Root does not compile, for example with the error CS0426 (the type name Root does not exist in the type), and the project has no generated file for the container (See the generated code shows where to look).
Causes and fixes:
- The compiler is too old. The generator needs the compiler of the .NET SDK the requirements name. An older compiler does not load it and warns instead, typically with CS9057. Install that SDK or a later one, and check
global.jsonif the repository pins an older one. - The IDE compiler is older. When
dotnet buildsucceeds but the IDE shows the error, see The IDE shows errors butdotnet buildpasses. - The language version is too old. Awaiten needs C# 11, or C# 9 or 10 when the project uses only the
typeofforms of the attributes and no module of it declares a[Scan]. The examples in these docs use C# 12. Projects that target .NET Framework or netstandard2.0 default to C# 7.3. Awaiten then reports AWT243 and generates nothing. Set<LangVersion>latest</LangVersion>in the project file. - The class is not
static partial. A container that is notstaticis AWT116. One that is notpartialfails with the compiler error CS0260, because the generated file declares the other part of the class.
The IDE shows errors but dotnet build passes
Symptom: The editor underlines Root, a resolve or an AWT id, while dotnet build on the command line succeeds, or the other way round.
Causes and fixes:
- The IDE compiler is older. Visual Studio and Rider compile with a compiler of their own. One older than the compiler of the SDK the requirements name does not load the generator, so the IDE sees no generated code. Update the IDE.
- The generator output is stale. An IDE can keep the generator it loaded first, for example after you update the Awaiten package or switch branches, and show results that no longer match the code. Rebuild the project; if the errors stay, restart the IDE.
dotnet buildalways runs the generator of the package version the project references.
A dependency cycle is reported
Symptom: The build fails with AWT102, and its message lists a chain of types that ends where it started, such as Till -> Printer -> Till.
Cause: Each type in the chain needs the next one to be constructed, so none of them can be built first. A constructor parameter, a factory or hook parameter and an [Inject] property all count as edges of the chain, also through several types in between.
Fix: One of these:
- Pick the edge that is needed only later, not during construction, and take
Func<T>instead ofTthere. - Make that edge an
[Inject(Deferred = true)]property. This needs a singleton or scoped type in the cycle, because a cycle of transients only never terminates (AWT145). See Break a cycle withDeferred. - Better, move the logic that both types share into a third type, so the two no longer need each other.
See AWT102.
A captive dependency is reported
Symptom: The build fails with AWT105, which names a singleton and a scoped service it depends on.
Cause: A singleton is built once on the root and lives as long as the container. A scoped service it took in its constructor, directly or through transients in between, would be held for that whole time instead of one scope, and would be shared by every scope. This is a captive dependency.
Fix: Give the consumer a lifetime no longer than the scoped service ([Scoped<EspressoMachine>]), or let it take Func<Owned<Order>> and build a fresh instance with its own scope per use. A Func<Order> on a singleton does not help: the singleton is built on the root, so the function returns the root's one scoped instance. See Lifetimes must line up.
Run time
Resolve throws "has registration errors; see the build diagnostics"
Symptom: Every Resolve, ResolveAsync, CreateScope and InitializeAsync on the container throws an InvalidOperationException that points at the build diagnostics, and TryResolve returns false.
Cause: The container has an error, such as a missing dependency, that was suppressed or lowered to a warning, so the build passed. A container with an error gets no wiring, only a placeholder Root that throws. One case is different: an error that a module of the container's own assembly reports about one of its registrations, lowered or disabled with #pragma where it is reported, only leaves that registration out.
Fix: Look for the AWT error in the build output and fix its cause instead of suppressing it. See the introduction of Diagnostics and A container with errors.
Resolve throws for an async-initialized service
Symptom: Resolve<T>() throws an InvalidOperationException saying the service requires asynchronous initialization, and TryResolve returns false, although the service is registered.
Cause: The service is async-initialized, or it depends on one without deferring, for example through a plain constructor parameter. In the strict default such a service has no synchronous path, so it is never handed out before it is initialized. A typed Resolve on the generated Root or Scope is reported at build time (AWT219).
Fix: One of these:
- Resolve it with
await shop.ResolveAsync<T>(cancellationToken), or inject an awaiting shape such asTask<T>orFunc<Task<T>>. See Async initialization. - When a framework forces a synchronous resolve, set
[Container(AsyncSafety = AsyncSafety.SyncAfterInit)]and callInitializeAsyncat startup. A synchronous resolve before warm-up then blocks until the initialization completes. See Relax the rule.
A disposable transient is withheld on the root
Symptom: Resolving a transient that implements IDisposable or IAsyncDisposable from the Root throws an InvalidOperationException that points at Owned<T>, and TryResolve returns false. Under the MS.DI bridge, resolving it from the root provider throws the same guidance.
Cause: Strict lifetime safety, the default. The root lives as long as the application, so every disposable transient it built would stay there undisposed until shutdown. A transient with an OnRelease hook is withheld the same way.
Fix: Resolve it from a scope (shop.CreateScope(), or an MS.DI scope), resolve or inject Owned<T> or Func<Owned<T>> and dispose the handle, or set LifetimeSafety = LifetimeSafety.Loose when you need the semantics of Microsoft.Extensions.DependencyInjection. See Owned.
A closing of an open generic is "not registered"
Symptom: Resolve<IRepository<Customer>>() throws "No registration for type" although [Transient(typeof(Repository<>), typeof(IRepository<>))] is registered. The build warns with AWT218 when the call is on the generated Root or Scope.
Cause: Awaiten builds only the closings that the graph asks for, through constructor parameters, factory and hook parameters, injected properties and the shapes over them. A closing you only resolve directly is never generated.
Fix: Register the closing explicitly next to the open registration, such as [Transient<Repository<Customer>, IRepository<Customer>>]. See Open generics.
A synchronous Dispose throws
Symptom: Dispose() on the root, a scope or an Owned<T> throws an InvalidOperationException about asynchronous disposal, or an AggregateException of several such exceptions when more than one service needs asynchronous disposal.
Cause: The container owns a service that implements IAsyncDisposable but not IDisposable, or one with an asynchronous OnRelease hook. A synchronous dispose cannot await them, so it disposes everything else and then throws. Awaiten warns at build time (AWT156) when the receiver is statically typed as the generated Root or Scope.
Fix: Dispose asynchronously, with await using or await shop.DisposeAsync(). Under the MS.DI bridge, dispose the provider and its scopes with DisposeAsync as well. See Async disposal.
A single service resolves to an unexpected implementation
Symptom: A service with several registrations resolves to another implementation than you expected.
Cause: In Awaiten the first registration wins single resolution, not the last one as in Microsoft.Extensions.DependencyInjection. The container's own registrations come first, then the imported modules in the order of their [Import]. All registrations stay members of the service's collections. Two imported modules that register different implementations of the service without Fallback are reported (AWT155) when something resolves the service singly, since only the import order decides which one it gets.
Fix: Register the implementation you want on the container itself, or make the other registrations overridable defaults with Fallback, which 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. When you want every implementation, depend on a collection. See Overridable defaults and Collections.
An imported service is missing at run time
Symptom: Resolving a service that depends on an [ImportService] or [ImportServices] type throws, naming the imported service.
Causes and fixes:
- The host does not register it. Awaiten cannot check the host's registrations at build time. Call
provider.VerifyAwaitenContainers()at startup, which throws and names every missing external dependency. See Verify external dependencies at startup. - No external resolver is wired yet. After a plain
AddAwaitenContainer<TRoot>(), the root is wired to the host's provider on the first resolution through a projected registration or whenWithInitialization()starts. Resolving from the root directly before that has no resolver to ask. See Providing the external resolver. Wire it earlier in one of these ways:- Use
AwaitenServiceProviderFactory, which wires the root when it builds the provider. - Construct the root with its resolver and pass it to
AddAwaitenContainer(root). - Call
SetExternalResolveryourself, throughIAwaitenExternalResolverHostor a variable typedIAwaitenRoot, since the root implements it explicitly. - For a standalone container, pass the resolver to the
Rootconstructor.
- Use
A container in another assembly fails after a library update
Symptom: After you update a library whose modules your container imports, the application throws a TypeLoadException or MissingMethodException, although the library's public API did not change.
Cause: A library module generates public factories and handles for the containers of other assemblies. They follow the library's internal types, so they are not a stable binary contract, and a container compiled against an older version of the library can call members that no longer exist.
Fix: Rebuild the container against each new version of the library. A package that ships a container importing a module of another package should depend on exactly the version of that package it was built against. See Shipping modules from a library.
A keyed service is "not registered"
Symptom: A keyed resolve throws, or a [FromKey] dependency is reported as missing, because the key does not match a registration.
Cause: Keys match by type and value, so a typo in a string key, or the string "Whole" against a registration keyed with MilkKind.Whole, selects nothing.
- At an injection site, Awaiten checks every key: a
[FromKey]key that nothing is registered under is a missing dependency (AWT101). - A keyed
Resolve,TryResolveorResolveAsyncwith a constant key on the generatedRootorScopeis checked too (AWT218). A call throughIAwaitenRoot, or with a key only known at run time, is checked at run time only:Resolvethrows andTryResolvereturnsfalse.
Fix: Use the key the registration declares. Prefer enum keys, which the C# compiler checks for typos. See Keyed services.
Frequently asked questions
Can I register services at run time?
No. The registrations are attributes that the generator reads at build time, and the generated code is the whole graph, so nothing can be added, replaced or removed while the app runs. To make a choice at run time:
- Pick the implementation in a factory method that takes the setting as a parameter.
- Register the variants under keys and choose one through a keyed dictionary.
- Switch a decorator on or off once per root with
When. - Leave services that the host registers at run time to the host and declare them with
[ImportService].
Can I have several containers in one app, or one per tenant?
Yes. Declare as many [Container] classes as you need. Share registrations through a module that each imports, or let one container extend another.
Every new MyContainer.Root() is an independent container with its own singletons, so one root per tenant works:
- Create the root when the tenant is first served and dispose it when the tenant goes away.
- Hand each root the services of its tenant through the
Root(IExternalResolver)constructor (see Providing the external resolver). - A pre-built
Instanceis a static member, so every root shares it.
How do I use ILogger<T> and IOptions<T>?
Under a host, logging and options stay registered in Microsoft.Extensions.DependencyInjection. Declare [ImportService(typeof(ILogger<>))] and [ImportService(typeof(IOptions<>))] on the container, and it asks the host's provider for the closing each consumer needs. See Pull every closing of a generic type.
Without a host, register what creates them like any other service, for example a Factory with a [RequestingType] parameter that creates a logger for the type that receives it.
Is resolution thread-safe?
Yes. You can resolve from the root and from any scope on several threads at once:
- A singleton is built exactly once, also under concurrent resolves, and every caller gets the same instance. A scoped service is built once per scope in the same way.
- Async initialization runs once per instance, and every concurrent caller awaits that one initialization.
- A transient is built for every resolve or injection.
The services themselves are your code: a singleton that several threads use has to be thread-safe on its own. Open a scope per unit of work, such as a request, and dispose it when the work is done.
Does it work with hot reload?
Awaiten is not tested with hot reload. The wiring is C# that the generator writes at build time, so a change to the registrations, such as a new attribute, another lifetime or a new constructor parameter, takes effect only after you rebuild and restart the app.
How do I publish with native AOT?
Set <PublishAot>true</PublishAot> in the project of the app and publish for a runtime identifier, such as dotnet publish -r linux-x64. Awaiten needs nothing else:
- The generated container constructs with
newand identifies types withtypeof. - Neither the
Awaitenlibrary nor the MS.DI bridge uses reflection to create services, so they need no trimming annotations or warning suppressions. - Your own code and your other dependencies have to be AOT-compatible too.
See What this gives you and Native AOT.
A diagnostic fires under TreatWarningsAsErrors. How do I keep it a warning?
Pick the scope you need, here for the warning AWT141:
- The project: exempt the id with
<WarningsNotAsErrors>$(WarningsNotAsErrors);AWT141</WarningsNotAsErrors>in the project file, or drop it with<NoWarn>. - Some files: set
dotnet_diagnostic.AWT141.severity = suggestionin an.editorconfigsection for them. - One place: surround it with
#pragma warning disable AWT141and#pragma warning restore AWT141, or put[SuppressMessage("Awaiten", "AWT141")]on the type or member where it is reported.
AWT118 under strict lifetime safety and AWT255 cannot be configured. Lowering or suppressing an error lets the build pass, but it does not make the container usable, as the introduction of Diagnostics explains.
Where to go next
- Diagnostics for every build-time check with an example and a fix.
- Runtime API for what each call throws or returns.