feat: initialize Awaiten containers on host startup via AddAwaitenInitialization#54
Conversation
🚀 Benchmark ResultsDetails
Details
Details
|
…tialization Add AddAwaitenInitialization, which registers an IHostedService that calls the generated container root's InitializeAsync on application startup, warming its async-initialized singletons in dependency order before the host begins serving, and honoring the host's startup CancellationToken. The companion now references Microsoft.Extensions.Hosting.Abstractions for IHostedService. A container that hosts async-initialized services through MS.DI pairs this with SyncResolveAfterInit so the warmed services can then be resolved synchronously through the relay. Adds a generic-host test across net8.0/net10.0/net48 (built with HostBuilder so it runs on net48) that observes the async singleton's initialization count directly - zero before startup, one after the host starts - so it proves the hosted service drove the warm-up rather than a later synchronous resolve, and updates the companion public-API baseline.
7920fd7 to
a728756
Compare
…registration The AddAwaitenInitialization warm-up resolves the container root directly (a pre-built singleton) rather than through a bridged registration, which is where a container's external ([FromServices] / [ImportServices]) dependencies are normally wired to the host's provider on first resolution. So the hosted service now wires the container's ExternalResolver to the host's root provider before warming - unless one was set explicitly - so an async-initialized singleton that draws on an external service can be constructed during startup instead of throwing. Registration now goes through TryAddEnumerable so registering the same container's initialization more than once is a no-op. Adds tests for the external-dependency warm-up, the de-duplication, and the null-argument guard. Resolves the Sonar findings: suppresses the S1144 false positive on the DI-reflection-instantiated hosted-service constructor, and makes the tests' static initialization counters internal (CA2211).
|
…host startup via `AddAwaitenInitialization` (#54) by Valentin Breuß
…host startup via `AddAwaitenInitialization` (#54) by Valentin Breuß
|
This is addressed in release v0.2.0. |



Add
AddAwaitenInitialization, which registers anIHostedServicethat calls the generated container root'sInitializeAsyncon application startup, warming its async-initialized singletons in dependency order before the host begins serving, and honoring the host's startupCancellationToken.The companion now references Microsoft.Extensions.Hosting.Abstractions for
IHostedService. A container that hosts async-initialized services through MS.DI pairs this withSyncResolveAfterInitso the warmed services can then be resolved synchronously through the relay.Adds a generic-host test across net8.0/net10.0/net48 (built with
HostBuilderso it runs on net48) that observes the async singleton's initialization count directly - zero before startup, one after the host starts - so it proves the hosted service drove the warm-up rather than a later synchronous resolve, and updates the companion public-API baseline.