microsoft/healthcare-shared-components
Publicmirrored from https://github.com/microsoft/healthcare-shared-componentsAvailable
src/Microsoft.Health.Api/Registration/HealthCheckRegistrationExtensions.cs
43lines · modecode
| 1 | // ------------------------------------------------------------------------------------------------- |
| 2 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. |
| 4 | // ------------------------------------------------------------------------------------------------- |
| 5 | |
| 6 | using System; |
| 7 | using EnsureThat; |
| 8 | using Microsoft.Extensions.DependencyInjection.Extensions; |
| 9 | using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 10 | using Microsoft.Extensions.Options; |
| 11 | using Microsoft.Health.Api.Features.HealthChecks; |
| 12 | |
| 13 | namespace Microsoft.Extensions.DependencyInjection; |
| 14 | |
| 15 | /// <summary> |
| 16 | /// A collection of methods for configuring health checks. |
| 17 | /// </summary> |
| 18 | public static class HealthCheckRegistrationExtensions |
| 19 | { |
| 20 | /// <summary> |
| 21 | /// Configures the global timeout for all health checks. |
| 22 | /// </summary> |
| 23 | /// <param name="services">The <see cref="IServiceCollection"/> containing the services.</param> |
| 24 | /// <param name="timeout">The global health check timeout.</param> |
| 25 | /// <returns>The <paramref name="services"/> for additional method invocations.</returns> |
| 26 | /// <exception cref="ArgumentNullException"> |
| 27 | /// <paramref name="services"/> is <see langword="null"/>. |
| 28 | /// </exception> |
| 29 | /// <exception cref="ArgumentOutOfRangeException"> |
| 30 | /// <paramref name="timeout"/> is negative. |
| 31 | /// </exception> |
| 32 | public static IServiceCollection ConfigureHealthCheckTimeout(this IServiceCollection services, TimeSpan timeout) |
| 33 | { |
| 34 | EnsureArg.IsNotNull(services, nameof(services)); |
| 35 | EnsureArg.IsGte(timeout, TimeSpan.Zero, nameof(timeout)); |
| 36 | |
| 37 | services.TryAdd( |
| 38 | ServiceDescriptor.Transient<IPostConfigureOptions<HealthCheckServiceOptions>, HealthCheckTimeoutPostConfigure>( |
| 39 | p => new HealthCheckTimeoutPostConfigure(timeout))); |
| 40 | |
| 41 | return services; |
| 42 | } |
| 43 | } |
| 44 | |