diff --git a/build/versions.props b/build/versions.props index 03d5d76370..33071faa66 100644 --- a/build/versions.props +++ b/build/versions.props @@ -17,7 +17,7 @@ 7.0.0 7.0.0 7.0.0 - 7.0.0 + 7.1.0 7.0.0 7.0.0 7.0.0 diff --git a/src/HealthChecks.DocumentDb/DocumentDbHealthCheck.cs b/src/HealthChecks.DocumentDb/DocumentDbHealthCheck.cs index 10974ba5cc..c174d0c228 100644 --- a/src/HealthChecks.DocumentDb/DocumentDbHealthCheck.cs +++ b/src/HealthChecks.DocumentDb/DocumentDbHealthCheck.cs @@ -7,12 +7,14 @@ namespace HealthChecks.DocumentDb; public class DocumentDbHealthCheck : IHealthCheck { private static readonly ConcurrentDictionary _connections = new(); - private readonly DocumentDbOptions _documentDbOptions = new(); + private readonly DocumentDbOptions _options; public DocumentDbHealthCheck(DocumentDbOptions documentDbOptions) { - _documentDbOptions.UriEndpoint = Guard.ThrowIfNull(documentDbOptions.UriEndpoint); - _documentDbOptions.PrimaryKey = Guard.ThrowIfNull(documentDbOptions.PrimaryKey); + Guard.ThrowIfNull(documentDbOptions.UriEndpoint); + Guard.ThrowIfNull(documentDbOptions.PrimaryKey); + + _options = documentDbOptions; } /// @@ -20,17 +22,25 @@ public async Task CheckHealthAsync(HealthCheckContext context { try { - if (!_connections.TryGetValue(_documentDbOptions.UriEndpoint, out var documentDbClient)) + if (!_connections.TryGetValue(_options.UriEndpoint, out var documentDbClient)) { - documentDbClient = new DocumentClient(new Uri(_documentDbOptions.UriEndpoint), _documentDbOptions.PrimaryKey); + documentDbClient = new DocumentClient(new Uri(_options.UriEndpoint), _options.PrimaryKey); - if (!_connections.TryAdd(_documentDbOptions.UriEndpoint, documentDbClient)) + if (!_connections.TryAdd(_options.UriEndpoint, documentDbClient)) { documentDbClient.Dispose(); - documentDbClient = _connections[_documentDbOptions.UriEndpoint]; + documentDbClient = _connections[_options.UriEndpoint]; } } - await documentDbClient.OpenAsync(cancellationToken).ConfigureAwait(false); + + if (!string.IsNullOrWhiteSpace(_options.DatabaseName) && !string.IsNullOrWhiteSpace(_options.CollectionName)) + { + await documentDbClient.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(_options.DatabaseName, _options.CollectionName)).ConfigureAwait(false); + } + else + { + await documentDbClient.OpenAsync(cancellationToken).ConfigureAwait(false); + } return HealthCheckResult.Healthy(); } diff --git a/src/HealthChecks.DocumentDb/DocumentDbOptions.cs b/src/HealthChecks.DocumentDb/DocumentDbOptions.cs index 7ee8437e3f..493656ed99 100644 --- a/src/HealthChecks.DocumentDb/DocumentDbOptions.cs +++ b/src/HealthChecks.DocumentDb/DocumentDbOptions.cs @@ -8,4 +8,8 @@ public class DocumentDbOptions public string UriEndpoint { get; set; } = null!; public string PrimaryKey { get; set; } = null!; + + public string? DatabaseName { get; set; } + + public string? CollectionName { get; set; } } diff --git a/test/HealthChecks.DocumentDb.Tests/DependencyInjection/RegistrationTests.cs b/test/HealthChecks.DocumentDb.Tests/DependencyInjection/RegistrationTests.cs index 7ae9cf5e87..d4792724fa 100644 --- a/test/HealthChecks.DocumentDb.Tests/DependencyInjection/RegistrationTests.cs +++ b/test/HealthChecks.DocumentDb.Tests/DependencyInjection/RegistrationTests.cs @@ -18,6 +18,7 @@ public void add_health_check_when_properly_configured() registration.Name.ShouldBe("documentdb"); check.ShouldBeOfType(); } + [Fact] public void add_named_health_check_when_properly_configured() { @@ -34,4 +35,27 @@ public void add_named_health_check_when_properly_configured() registration.Name.ShouldBe("my-documentdb-group"); check.ShouldBeOfType(); } + + [Fact] + public void add_health_check_when_properly_configured_with_database_name_and_collection_name() + { + var services = new ServiceCollection(); + services.AddHealthChecks() + .AddDocumentDb(_ => + { + _.PrimaryKey = "key"; + _.UriEndpoint = "endpoint"; + _.DatabaseName = "database"; + _.CollectionName = "collection"; + }); + + using var serviceProvider = services.BuildServiceProvider(); + var options = serviceProvider.GetRequiredService>(); + + var registration = options.Value.Registrations.First(); + var check = registration.Factory(serviceProvider); + + registration.Name.ShouldBe("documentdb"); + check.ShouldBeOfType(); + } } diff --git a/test/HealthChecks.DocumentDb.Tests/HealthChecks.DocumentDb.approved.txt b/test/HealthChecks.DocumentDb.Tests/HealthChecks.DocumentDb.approved.txt index 5adaf80371..788128e141 100644 --- a/test/HealthChecks.DocumentDb.Tests/HealthChecks.DocumentDb.approved.txt +++ b/test/HealthChecks.DocumentDb.Tests/HealthChecks.DocumentDb.approved.txt @@ -8,6 +8,8 @@ namespace HealthChecks.DocumentDb public class DocumentDbOptions { public DocumentDbOptions() { } + public string? CollectionName { get; set; } + public string? DatabaseName { get; set; } public string PrimaryKey { get; set; } public string UriEndpoint { get; set; } }