ASP.NET Core Web API

Health Checks & Diagnostics

1 Views Updated 5/4/2026

Health Checks & Diagnostics

When your Web API is deployed to a cloud environment like Kubernetes or Azure App Service, the infrastructure needs to know if your API is actually "alive" or if it has crashed silently. Health Checks provide dedicated endpoints for the load balancer to continuously ping.

1. Basic Health Checks

By default, ASP.NET Core can provide a simple "Liveness" probe returning an HTTP 200 "Healthy" string.

var builder = WebApplication.CreateBuilder(args);

// 1. Register the Health Checks service
builder.Services.AddHealthChecks();

var app = builder.Build();

// 2. Map the endpoint (Load balancers will ping URL/health)
app.MapHealthChecks("/health");

app.Run();

2. Advanced "Readiness" Probes (Database Verification)

A server might be "alive", but if the SQL Database it depends on goes offline, the API is functionally useless. We must configure the Health Check to actually ping the database before reporting "Healthy".

# Install the official SQL Server Health Check package
dotnet add package AspNetCore.HealthChecks.SqlServer

Wiring the DB Ping

builder.Services.AddHealthChecks()
    // Instantly checks if the connection string works!
    .AddSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));

// Now, if you visit /health and SQL Server is down, 
// the API returns an HTTP 503 Service Unavailable automatically.

3. Human-Readable Health Check UI

Returning a plain-text "Healthy" or "Unhealthy" string is great for Kubernetes, but humans prefer a dashboard. Using the AspNetCore.HealthChecks.UI community package, you can generate a beautiful dashboard showing the real-time status of your API, DB, and Redis caches.

// 1. Add services
builder.Services.AddHealthChecksUI().AddInMemoryStorage();

// 2. Map the UI endpoint
app.MapHealthChecksUI(options => 
{
    // The beautiful dashboard will live at URL/health-ui
    options.UIPath = "/health-ui";
    options.AddCustomStylesheet("wwwroot/custom-ui.css");
});

4. Interview Mastery

Q: "In Kubernetes, there is a strict distinction between a 'Liveness Probe' and a 'Readiness Probe'. How do we configure ASP.NET Core Health Checks to satisfy both requirements distinctly?"

Architect Answer: "A Liveness Probe asks 'Is the .NET process physically running?'. If it fails, Kubernetes restarts the pod. A Readiness Probe asks 'Is the API ready to accept traffic?'. If it fails, Kubernetes stops sending users to it, but doesn't restart it. We accomplish this using Health Check Tags. We create two separate `MapHealthChecks()` endpoints. `MapHealthChecks('/health/live')` contains NO database checks. It just returns 'Alive' instantly. `MapHealthChecks('/health/ready', new HealthCheckOptions { Predicate = check => check.Tags.Contains('db') })` specifically runs the SQL Server ping test. This prevents Kubernetes from infinitely restarting our server pod just because an external SQL server temporarily went offline."

ASP.NET Core Web API
1. Fundamentals & HTTP
Introduction to ASP.NET Core Web API REST Principles and HTTP Methods Controllers & ControllerBase Routing (Attribute vs Conventional) Action Return Types (IActionResult)
2. Request Handling
Model Binding (FromQuery, FromBody, FromRoute) Dependency Injection (DI) Deep Dive App Settings & The Options Pattern
3. Data Access & Architecture
EF Core Setup in Web API DbContext & Migrations Repository & Unit of Work Pattern Asynchronous Programming (async/await)
4. Data Transfer & Validation
Data Transfer Objects (DTOs) & AutoMapper Model Validation (DataAnnotations) FluentValidation Integration
5. Advanced Concepts
Global Exception Handling Middleware Content Negotiation (JSON vs XML) Pagination & Filtering Advanced Searching & Sorting HATEOAS (Hypermedia) Implementation Output Caching & Response Caching
6. Security & Authorization
Cross-Origin Resource Sharing (CORS) JWT Authentication Setup Access Tokens & Refresh Tokens Workflow Role-Based & Policy-Based Authorization API Key Authentication Rate Limiting & Throttling
7. Documentation & Testing
Swagger & OpenAPI Configuration Customizing API Documentation Unit Testing Controllers (xUnit & Moq) Integration Testing (WebApplicationFactory)
8. Microservices & Deployment
Consuming External APIs (IHttpClientFactory) Health Checks & Diagnostics API Versioning Strategies Deploying APIs (Docker & Azure)