@@ -231,27 +231,52 @@ public static IServiceCollection AddSqlServerHealthChecks(this IServiceCollectio
231231 return services ;
232232 }
233233
234- public static IEndpointRouteBuilder AddDatabaseHealthChecks ( this IEndpointRouteBuilder builder , string pattern )
234+ public static IEndpointRouteBuilder AddDatabaseHealthChecks ( this IEndpointRouteBuilder builder , string pattern , bool allowAnonymous = false )
235235 {
236- builder . MapHealthChecks ( pattern , new HealthCheckOptions
236+ if ( allowAnonymous )
237237 {
238- ResponseWriter = async ( context , report ) =>
238+ builder . MapHealthChecks ( pattern , new HealthCheckOptions
239239 {
240- var result = JsonSerializer . Serialize ( new
240+ ResponseWriter = async ( context , report ) =>
241241 {
242- status = report . Status . ToString ( ) ,
243- details = report . Entries . Select ( e => new
242+ var result = JsonSerializer . Serialize ( new
244243 {
245- service = e . Key ,
246- status = Enum . GetName ( typeof ( HealthStatus ) , e . Value . Status ) ,
247- description = e . Value . Description
248- } )
249- } ) ;
250-
251- context . Response . ContentType = MediaTypeNames . Application . Json ;
252- await context . Response . WriteAsync ( result ) ;
253- }
254- } ) ;
244+ status = report . Status . ToString ( ) ,
245+ details = report . Entries . Select ( e => new
246+ {
247+ service = e . Key ,
248+ status = Enum . GetName ( typeof ( HealthStatus ) , e . Value . Status ) ,
249+ description = e . Value . Description
250+ } )
251+ } ) ;
252+
253+ context . Response . ContentType = MediaTypeNames . Application . Json ;
254+ await context . Response . WriteAsync ( result ) ;
255+ }
256+ } ) ;
257+ }
258+ else
259+ {
260+ builder . MapHealthChecks ( pattern , new HealthCheckOptions
261+ {
262+ ResponseWriter = async ( context , report ) =>
263+ {
264+ var result = JsonSerializer . Serialize ( new
265+ {
266+ status = report . Status . ToString ( ) ,
267+ details = report . Entries . Select ( e => new
268+ {
269+ service = e . Key ,
270+ status = Enum . GetName ( typeof ( HealthStatus ) , e . Value . Status ) ,
271+ description = e . Value . Description
272+ } )
273+ } ) ;
274+
275+ context . Response . ContentType = MediaTypeNames . Application . Json ;
276+ await context . Response . WriteAsync ( result ) ;
277+ }
278+ } ) . AllowAnonymous ( ) ;
279+ }
255280
256281 return builder ;
257282 }
0 commit comments