Search documentationEsc

Health checks

ASP.NET Core health checks for Cosmos DB powered by Cosmos Repository's container metadata.

IEvangelist.Azure.CosmosRepository.AspNetCore adds support for ASP.NET Core health checks using the HealthChecks.CosmosDb package.

Setup

Add the Cosmos DB health check:

services.AddHealthChecks().AddCosmosRepository();

By default this scans every assembly in your solution to locate the container names registered for each IItem type. To narrow the scan and reduce startup time, pass the assemblies that contain your items:

services.AddHealthChecks()
.AddCosmosRepository(assemblies: typeof(ExampleItem).Assembly);

Alternatively, supply an AzureCosmosDbHealthCheckOptions factory and specify the containers to check explicitly:

services.AddHealthChecks()
.AddCosmosRepository(optionsFactory: sp => new AzureCosmosDbHealthCheckOptions
{
DatabaseId = "my-database",
ContainerIds = new[] { "Container1", "Container2" }
});

Pass optionsFactory: null to fall back to the default behavior of the HealthChecks.CosmosDb package (a single CosmosClient.ReadAccountAsync() call):

services.AddHealthChecks().AddCosmosRepository(optionsFactory: null);

The health check supports the full ASP.NET Core feature set — failureStatus, tags, and so on. See the official docs for configuration details.

Don’t forget to map the endpoint:

app.MapHealthChecks("/healthz");

Container initialization & health checks

By default Cosmos DB containers are created lazily on first repository access. That can cause health checks to fail during startup while containers are still being created.

If you’re seeing startup failures, switch to eager initialization so containers exist before the app starts handling requests. See Container initialization for the full details.