liveBook · Manning

Service discovery

As explained in Building Distributed Applications that Work

Service discovery is a mechanism by which services locate the network endpoints of other services dynamically rather than relying on fixed addresses.

A storefront calling a catalog API needs an address, but that address or port may change as independently running services move. Service discovery lets the storefront use the API’s registered name instead of hardcoding its network location. In Aspire, the AppHost declares the services and their relationships; service defaults equip the applications to resolve those names. section 1.0 section 1.9.3

More generally, discovery involves registering a service’s location, querying for it, and updating that information as services join or leave. Load balancing is closely related but is not itself service discovery. section 1.9

To use named HTTP endpoints in an Aspire application:

  • Declare the API as a named project in AppHost and give the storefront a reference to it. section 10.2 section 1.10
  • Add Microsoft.Extensions.ServiceDiscovery to the service-defaults project. In AddServiceDefaults(), call builder.Services.AddServiceDiscovery() and configure HTTP clients with http.AddServiceDiscovery(). section 1.9.1 section 1.9.2
  • Call AddServiceDefaults() in the application, then set the HTTP client’s base address to the API’s registered name rather than an IP address. section 1.9.3
client.BaseAddress = new("https+http://apiservice");

The storefront client uses the registered name apiservice. The https+http scheme prefers HTTPS and falls back to HTTP; the client does not need the API’s current address or port. section 1.9.3

Not every integration accepts a discovery-aware HTTP client automatically. For a SignalR connection, pass an IHttpMessageHandlerFactory into the connection builder and assign its handler to the connection options; a URL naming the Aspire-hosted API can then be resolved. An Ollama integration offers another boundary: a discovery-enabled HTTP client can resolve its container name, but the Semantic Kernel connector shown here requires an endpoint extracted from an Aspire-supplied connection string. section 4.4.2 section 5.2.1

Browser-facing SSO redirects need standard, publicly accessible URLs, not an https+http service name understood only by the discovery library. For local development, AppHost can obtain the storefront’s HTTP or HTTPS endpoint and pass its URL to the identity provider through environment variables. That endpoint-reading code is restricted to development because hosted service addresses may not yet be available at the same point on a real host. section 8.2

How Building Distributed Applications that Work covers it

How deep it goes
Defines registration and querying, then implements named HTTP calls and integration workarounds; does not detail registry internals.
Examples
Uses a storefront calling an API, a SignalR order-tracking hub, an Ollama container, and identity-provider redirects.
What it assumes
Assumes familiarity with ASP.NET Core startup and HTTP clients while introducing Aspire service defaults and named endpoint resolution.
What you can do after
Apply it via Aspire service defaults to configure named HTTP calls and recognize when an integration needs a standard URL or custom handler.
How it connects
Connects Aspire AppHost, service defaults, HTTP clients, SignalR, and single sign-on.

Also explained in

Compare how 7 books explain service discovery →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Distributed Applications that Work. The section links point to the parts of the book it draws on.