Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overload to HealthChecks for configuring options #25238

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
Expand All @@ -12,6 +13,25 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
public static class HealthCheckServiceCollectionExtensions
{
/// <summary>
/// Adds the <see cref="HealthCheckService"/> to the container, using the provided delegate to register
/// health checks.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="HealthCheckService"/> to.</param>
/// <param name="configureOptions">A delegate to configure the <see cref="HealthCheckServiceOptions"/>.</param>
/// <returns>An instance of <see cref="IHealthChecksBuilder"/> from which health checks can be registered.</returns>
public static IHealthChecksBuilder AddHealthChecks(this IServiceCollection services, Action<HealthCheckServiceOptions> configureOptions)
{
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}

services.Configure(configureOptions);

return services.AddHealthChecks();
}

/// <summary>
/// Adds the <see cref="HealthCheckService"/> to the container, using the provided delegate to register
/// health checks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Xunit;

namespace Microsoft.Extensions.DependencyInjection
Expand Down Expand Up @@ -42,6 +43,27 @@ public void AddHealthChecks_RegistersSingletonHealthCheckServiceIdempotently()
});
}

[Fact]
public void AddHealthChecks_CallsConfigureOptions()
{
// Arrange
var isCalled = false;
var services = new ServiceCollection();
services.AddHealthChecks(options =>
{
isCalled = true;
});

var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetService<IOptions<HealthCheckServiceOptions>>();

// Act
_ = options?.Value;

// Assert
Assert.True(isCalled);
}

[Fact] // see: https://github.com/dotnet/extensions/issues/639
public void AddHealthChecks_RegistersPublisherService_WhenOtherHostedServicesRegistered()
{
Expand Down