fix: initialize lifecycle on hosts without IHostedLifecycleService support#787
fix: initialize lifecycle on hosts without IHostedLifecycleService support#787askpt wants to merge 3 commits into
Conversation
…pport The legacy ASP.NET Core WebHost only invokes IHostedService.StartAsync and StopAsync, never the IHostedLifecycleService callbacks. With the default FeatureLifecycleStateOptions (StartState=Starting, StopState=Stopped), providers were never initialized nor shut down on such hosts. HostedFeatureLifecycleService now detects whether the host invoked StartingAsync and, when it did not, falls back to StartAsync/StopAsync to run the configured initialization and shutdown. Fixes #773 Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthrough
ChangesHosted lifecycle fallback
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Host
participant HostedFeatureLifecycleService
participant IFeatureLifecycleManager
Host->>HostedFeatureLifecycleService: Invoke StartingAsync, StoppedAsync, StartAsync, or StopAsync
HostedFeatureLifecycleService->>HostedFeatureLifecycleService: Check callback support and configured lifecycle state
HostedFeatureLifecycleService->>IFeatureLifecycleManager: EnsureInitializedAsync or ShutdownAsync
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #787 +/- ##
==========================================
+ Coverage 93.18% 93.55% +0.37%
==========================================
Files 66 66
Lines 2993 3010 +17
Branches 376 378 +2
==========================================
+ Hits 2789 2816 +27
+ Misses 140 130 -10
Partials 64 64 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Adds generic-host stop timing coverage per stop state, a shutdown test for hosts stopping services before StartAsync ever ran, and clarifies in the README that the fallback preserves lifecycle execution rather than exact callback timing on IHostedService-only hosts. Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com>
This PR
Fixes provider initialization and shutdown never running on hosts that only support
IHostedService, such as the legacy ASP.NET CoreWebHost/WebHostBuilder.Why
The legacy
WebHostnever invokes theIHostedLifecycleServicecallbacks (StartingAsync/StartedAsync/StoppingAsync/StoppedAsync), and upstream support was declined (dotnet/aspnetcore#60176). SinceFeatureLifecycleStateOptionsdefaults toStartState = StartingandStopState = Stopped,HostedFeatureLifecycleServicewaited for callbacks that never fired, so providers were never registered nor gracefully shut down on such hosts.How
HostedFeatureLifecycleServicenow records whether the host invokedStartingAsync(the generic host always calls it beforeStartAsync). When it was never invoked,StartAsync/StopAsyncfall back to running the configured initialization/shutdown, covering allFeatureStartState/FeatureStopStatevalues. An informational log is emitted when the fallback engages.WebApplication.CreateBuilder,Host.CreateDefaultBuilder) is unchanged, and initialization still runs at most once.HostedFeatureLifecycleServiceTestscovering generic-host callback sequences andIHostedService-only sequences for every start/stop state.FeatureLifecycleStateOptionsconfiguration in the Hosting README.Notes
Fixes #773
Related Issues: dotnet/aspnetcore#60176
Follow-up Tasks
N/A
How to test
Run the hosting test suite:
dotnet test test/OpenFeature.Hosting.Tests. To reproduce the original bug, host OpenFeature in an app built withWebHost.CreateDefaultBuilderand observe that providers now initialize with default options.