Service discovery and health
Service discovery and health
Service discovery resolves a logical service name to an address appropriate for the environment. Health checks expose whether a process is alive or ready to perform work; discovery alone does not establish health or authorization.
Aspire can pass resource endpoint information via WithReference. A .NET consumer may register service discovery with AddServiceDiscovery and configure its HttpClient builder to resolve a logical address such as https+http://inventory. That compound scheme depends on a resolver; it is not a generally usable ordinary HTTP URI. The actual lab uses a plain HTTP loopback endpoint and a pinned service discovery library; production requires authenticated TLS as appropriate, restricted access and approved endpoint resolution. Do not trust a URL supplied by an arbitrary user. Liveness answers whether a process is functioning; readiness answers whether it can accept its intended traffic. A temporary database outage should not necessarily cause endless restarts. A startup probe can allow a slow initial boot. ASP.NET Core health checks can be tagged and filtered; default HTTP mapping includes 503 for Unhealthy and 200 for Degraded, unless configured differently. A readiness policy that requires every dependency to be healthy may deliberately map Degraded to 503. A report aggregates only the checks that actually ran; a filter matching no checks can yield Healthy. Verify required check registrations and tag coverage, rather than assuming that a healthy empty report proves readiness. Health endpoints should expose minimal details and be protected or network-scoped. Avoid expensive checks and probe storms; use bounded timeouts. Service defaults may include telemetry, health checks, discovery and HTTP resilience; inspect the actual generated code and adapt retry policies to operation semantics.
Baseline: .NET 10 / C# 14. All C# results below are reasoned, not executed. Offline models and the separate Aspire lab are explicitly distinguished.
Resolve a logical name without networking
Use an explicit local allowlist as an illustrative resolver, not the Microsoft discovery implementation.
Use the archived Fixture.csproj with an approved .NET 10 SDK. No third-party packages are required. Inspect the project, then build and run with the approved SDK.
using System;using System.Collections.Generic;var endpoints = new Dictionary<string, Uri>(StringComparer.Ordinal) { ["inventory"] = new("http://127.0.0.1:5101/")};foreach (string name in new[] { "inventory", "unknown" }) { Console.WriteLine(endpoints.TryGetValue(name, out var address) ? address.AbsoluteUri : "unresolved");}Expected result (not executed):
http://127.0.0.1:5101/unresolvedThe dictionary models logical names and fail-closed lookup. No HTTP request occurs; actual resolver configuration and endpoint security must be tested separately.
Run real health-check registrations in process
This uses the ASP.NET Core shared framework, but no server. One ready-tagged dependency is intentionally unhealthy; liveness filters it out.
Use the archived Fixture.csproj with an approved .NET 10 SDK. No third-party packages are required. Inspect the project, then build and run with the approved SDK. Requires the ASP.NET Core 10 shared framework.
using System;using System.Linq;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Diagnostics.HealthChecks;var services = new ServiceCollection();services.AddLogging();services.AddHealthChecks() .AddCheck("self", () => HealthCheckResult.Healthy(), tags: new[] { "live" }) .AddCheck("inventory", () => HealthCheckResult.Unhealthy(), tags: new[] { "ready" });using var provider = services.BuildServiceProvider();var checks = provider.GetRequiredService<HealthCheckService>();Console.WriteLine((await checks.CheckHealthAsync(r => r.Tags.Contains("live"))).Status);Console.WriteLine((await checks.CheckHealthAsync()).Status);Expected result (not executed):
HealthyUnhealthyThe liveness subset is healthy but the whole readiness set is unhealthy. A mapped endpoint could use these reports to return different statuses. This is actual health-check API source, not a running HTTP endpoint.
Model the effect of outage and recovery
A healthy process can temporarily be unready. Model an HTTP readiness mapping with explicit states.
Use the archived Fixture.csproj with an approved .NET 10 SDK. No third-party packages are required. Inspect the project, then build and run with the approved SDK.
using System;foreach (var state in new[] { (true, true), (true, false), (true, true) }) { bool live = state.Item1, ready = live && state.Item2; Console.WriteLine($"live={live}; ready={(ready ? 200 : 503)}");}Expected result (not executed):
live=True; ready=200live=True; ready=503live=True; ready=200No HTTP response is sent in this model. Real probes need endpoint checks, timeout handling, and verification of platform-specific routing and restart behavior.
Apply the concept
Design an outage drill with a healthy process, failing inventory dependency and recovery. Specify separate liveness and readiness results, a timeout and what the client should see.
Use the manual practice and knowledge check. Do not deploy or publish these fixtures.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Service discovery and health — knowledge check
Apply the lesson concepts to the following scenarios.
What does service discovery supply?
When should a dependency outage usually make a service unready?
What is the default HTTP health status for Unhealthy in ASP.NET Core?
What is a danger of using dependency health as liveness indiscriminately?
What does an in-process health report test not establish?