File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Threading ;
3+ using System . Threading . Tasks ;
4+ using Microsoft . Extensions . Diagnostics . HealthChecks ;
5+ using Microsoft . Extensions . Hosting ;
6+
7+ namespace blog_zero_downtime_with_health_checks
8+ {
9+ public class ShuttingDownHealthCheck : IHealthCheck
10+ {
11+ private HealthStatus _status = HealthStatus . Healthy ;
12+
13+ public ShuttingDownHealthCheck ( IHostApplicationLifetime appLifetime )
14+ {
15+ appLifetime . ApplicationStopping . Register ( ( ) =>
16+ {
17+ Console . WriteLine ( "Shutting down" ) ;
18+ _status = HealthStatus . Unhealthy ;
19+ } ) ;
20+ }
21+
22+ public Task < HealthCheckResult > CheckHealthAsync (
23+ HealthCheckContext context ,
24+ CancellationToken cancellationToken = default )
25+ {
26+ var result = new HealthCheckResult ( _status ) ;
27+ return Task . FromResult ( result ) ;
28+ }
29+ }
30+ }
Original file line number Diff line number Diff line change @@ -16,7 +16,10 @@ public class Startup
1616 // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
1717 public void ConfigureServices ( IServiceCollection services )
1818 {
19- services . AddHealthChecks ( ) ;
19+ services . AddSingleton < ShuttingDownHealthCheck > ( ) ;
20+
21+ services . AddHealthChecks ( )
22+ . AddCheck < ShuttingDownHealthCheck > ( "shutting_down" ) ;
2023 }
2124
2225 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
You can’t perform that action at this time.
0 commit comments