GrpcHealthChecksServiceExtensions.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #region Copyright notice and license
  2. // Copyright 2019 The gRPC Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using Grpc.AspNetCore.HealthChecks;
  18. using Grpc.HealthCheck;
  19. using Microsoft.Extensions.DependencyInjection.Extensions;
  20. using Microsoft.Extensions.Diagnostics.HealthChecks;
  21. namespace Microsoft.Extensions.DependencyInjection
  22. {
  23. /// <summary>
  24. /// Extension methods for the gRPC health checks services.
  25. /// </summary>
  26. public static class GrpcHealthChecksServiceExtensions
  27. {
  28. /// <summary>
  29. /// Adds gRPC health check services to the specified <see cref="IServiceCollection" />.
  30. /// </summary>
  31. /// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
  32. /// <returns>An instance of <see cref="IHealthChecksBuilder"/> from which health checks can be registered.</returns>
  33. public static IHealthChecksBuilder AddGrpcHealthChecks(this IServiceCollection services)
  34. {
  35. if (services == null)
  36. {
  37. throw new ArgumentNullException(nameof(services));
  38. }
  39. // HealthServiceImpl is designed to be a singleton
  40. services.TryAddSingleton<HealthServiceImpl>();
  41. services.TryAddEnumerable(ServiceDescriptor.Singleton<IHealthCheckPublisher, GrpcHealthChecksPublisher>());
  42. return services.AddHealthChecks();
  43. }
  44. }
  45. }