Skip to content

feat: initialize Awaiten containers on host startup via AddAwaitenInitialization#54

Merged
vbreuss merged 2 commits into
mainfrom
feat/host-initialization
Jul 3, 2026
Merged

feat: initialize Awaiten containers on host startup via AddAwaitenInitialization#54
vbreuss merged 2 commits into
mainfrom
feat/host-initialization

Conversation

@vbreuss

@vbreuss vbreuss commented Jul 3, 2026

Copy link
Copy Markdown
Member

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.

@vbreuss vbreuss self-assigned this Jul 3, 2026
@vbreuss vbreuss added the enhancement New feature or request label Jul 3, 2026
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

Test Results

   18 files  ± 0     18 suites  ±0   2m 57s ⏱️ -1s
  576 tests + 4    575 ✅ + 4  1 💤 ±0  0 ❌ ±0 
2 914 runs  +28  2 913 ✅ +28  1 💤 ±0  0 ❌ ±0 

Results for commit 389d820. ± Comparison against base commit eb8e5d5.

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

👽 Mutation Results

Mutation testing badge

Awaiten

Details
File Score Killed Survived Timeout No Coverage Ignored Compile Errors Runtime Errors Total Detected Total Undetected Total Mutants

The final mutation score is NaN%

Coverage Thresholds: high:80 low:60 break:0

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 7763 2.87GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.301
[Host] : .NET 10.0.9 (10.0.9, 10.0.926.27113), X64 RyuJIT x86-64-v3

Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=1 WarmupCount=10

Resolve Size Mean Error StdDev Ratio Allocated Alloc Ratio
baseline* 8 8.102 ns 0.0061 ns 0.0054 ns 1.00 - NA
Awaiten 8 8.091 ns 0.0064 ns 0.0050 ns 1.00 - NA
MsDI 8 7.496 ns 0.1293 ns 0.1210 ns 0.93 - NA
Autofac 8 138.594 ns 2.8219 ns 2.5016 ns 17.13 656 B NA
Jab 8 2.650 ns 0.0052 ns 0.0043 ns 0.33 - NA
PureDI 8 5.427 ns 0.0105 ns 0.0094 ns 0.67 - NA
DryIoc 8 8.761 ns 0.0170 ns 0.0142 ns 1.08 - NA
SimpleInjector 8 10.690 ns 0.0111 ns 0.0099 ns 1.32 - NA
baseline* 256 8.757 ns 0.0063 ns 0.0055 ns 1.00 - NA
Awaiten 256 8.762 ns 0.0174 ns 0.0163 ns 1.00 - NA
MsDI 256 7.243 ns 0.0116 ns 0.0097 ns 0.83 - NA
Autofac 256 136.153 ns 3.2740 ns 3.0625 ns 15.54 656 B NA
Jab 256 42.749 ns 0.0795 ns 0.0705 ns 4.88 - NA
PureDI 256 7.334 ns 0.0052 ns 0.0044 ns 0.84 - NA
DryIoc 256 8.712 ns 0.0068 ns 0.0053 ns 0.99 - NA
SimpleInjector 256 14.050 ns 0.0108 ns 0.0084 ns 1.60 - NA
Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 7763 2.45GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.301
[Host] : .NET 10.0.9 (10.0.9, 10.0.926.27113), X64 RyuJIT x86-64-v3

Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=1 WarmupCount=10

Realistic Mean Error StdDev Ratio Allocated Alloc Ratio
baseline* 267.4 ns 1.84 ns 1.72 ns 1.15 568 B 1.00
Awaiten 232.0 ns 1.35 ns 1.27 ns 1.00 568 B 1.00
MsDI 590.0 ns 6.66 ns 5.90 ns 2.54 1104 B 1.94
Autofac 8,505.6 ns 87.56 ns 81.90 ns 36.67 13696 B 24.11
Jab 178.2 ns 2.79 ns 2.61 ns 0.77 432 B 0.76
DryIoc 409.1 ns 4.42 ns 4.13 ns 1.76 944 B 1.66
SimpleInjector 709.7 ns 2.79 ns 2.48 ns 3.06 1096 B 1.93
PureDI 172.9 ns 1.28 ns 1.13 ns 0.75 632 B 1.11
Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.301
[Host] : .NET 10.0.9 (10.0.9, 10.0.926.27113), X64 RyuJIT x86-64-v4

Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=1 WarmupCount=10

Build Size Mean Error StdDev Ratio Allocated Alloc Ratio
baseline* 8 16.92 ns 0.145 ns 0.121 ns 1.24 144 B 1.00
Awaiten 8 13.658 ns 0.1347 ns 0.1194 ns 1.00 144 B 1.00
MsDI 8 1,176.747 ns 25.1478 ns 23.5233 ns 86.16 5688 B 39.50
Autofac 8 26,245.060 ns 259.6322 ns 242.8601 ns 1,921.74 33094 B 229.82
Jab 8 6.643 ns 0.1269 ns 0.1125 ns 0.49 96 B 0.67
PureDI 8 11.806 ns 0.3246 ns 0.3036 ns 0.86 128 B 0.89
DryIoc 8 587.139 ns 9.5126 ns 7.9435 ns 42.99 1528 B 10.61
SimpleInjector 8 9,100.803 ns 188.9950 ns 176.7861 ns 666.39 24760 B 171.94
baseline* 256 91.24 ns 0.586 ns 0.519 ns 1.46 2128 B 1.00
Awaiten 256 62.494 ns 0.3047 ns 0.2701 ns 1.00 2128 B 1.00
MsDI 256 10,401.714 ns 98.2768 ns 87.1198 ns 166.45 61016 B 28.67
Autofac 256 618,642.697 ns 4,989.7780 ns 4,667.4414 ns 9,899.36 737064 B 346.36
Jab 256 51.120 ns 0.1749 ns 0.1460 ns 0.82 2080 B 0.98
PureDI 256 58.408 ns 0.1387 ns 0.1229 ns 0.93 2112 B 0.99
DryIoc 256 36,901.850 ns 648.5886 ns 606.6902 ns 590.49 80998 B 38.06
SimpleInjector 256 270,464.301 ns 6,656.3267 ns 6,226.3321 ns 4,327.90 573060 B 269.30

baseline* rows show the corresponding Awaiten benchmark from the most recent successful main branch build with results, for regression comparison.

…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.
@vbreuss vbreuss force-pushed the feat/host-initialization branch from 7920fd7 to a728756 Compare July 3, 2026 18:53
…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).
@sonarqubecloud

sonarqubecloud Bot commented Jul 3, 2026

Copy link
Copy Markdown

@vbreuss vbreuss merged commit 7e530a7 into main Jul 3, 2026
14 checks passed
@vbreuss vbreuss deleted the feat/host-initialization branch July 3, 2026 19:02
github-actions Bot added a commit that referenced this pull request Jul 3, 2026
…host startup via `AddAwaitenInitialization` (#54) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Jul 3, 2026
…host startup via `AddAwaitenInitialization` (#54) by Valentin Breuß
@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown

This is addressed in release v0.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request state: released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant