How Service Discovery Works
When you use.WithReference() in your AppHost, Aspire automatically configures service discovery between resources:
- Injects service discovery environment variables into the frontend
- Configures the frontend to resolve service endpoints at runtime
- Handles different network topologies (containers, host processes)
IResourceWithServiceDiscovery Interface
Resources that support service discovery implement theIResourceWithServiceDiscovery interface:
ProjectResource(.NET projects)ContainerResource(Docker containers)ExecutableResource(standalone executables)- Specialized resources (Redis, PostgreSQL, RabbitMQ, etc.)
Environment Variable Injection
When you reference a service, Aspire injects structured environment variables following a standard naming pattern:Consuming Service Discovery
In your .NET service projects, consume service discovery by adding service defaults:Program.cs
ServiceDefaults Implementation
TheAddServiceDefaults() extension configures service discovery automatically:
Extensions.cs (ServiceDefaults project)
Service Name Resolution
Aspire uses a URI scheme pattern for service discovery:| Scheme | Meaning |
|---|---|
http://servicename | HTTP only |
https://servicename | HTTPS only |
https+http://servicename | Prefer HTTPS, fallback to HTTP |
- Looks up the service name in configuration
- Resolves to actual endpoint (localhost:port, container name, etc.)
- Selects the appropriate protocol
Context-Based Endpoint Resolution
Aspire automatically resolves endpoints differently based on the execution context:Container-to-Container Communication
When both source and target are containers:redis:6379 (uses Docker container network)
Container-to-Project Communication
When a container references a .NET project:host.docker.internal:5000 (container accesses host network)
Project-to-Container Communication
When a .NET project references a container:localhost:6379 (project accesses container’s mapped port)
Resolution Rules Summary
| Source | Target | Resolution |
|---|---|---|
| Container | Container | resource-name:port |
| Container | Project/Executable | host.docker.internal:port |
| Project/Executable | Container | localhost:mapped-port |
| Project/Executable | Project/Executable | localhost:port |
Database Connection Strings
Database resources implementIResourceWithConnectionString and provide structured connection information:
Consuming Connection Strings
Access connection strings in your application:ConnectionStrings:catalogdb configuration key.
Endpoint References
Get direct references to specific endpoints:Endpoint Properties
Access specific endpoint properties usingEndpointReference:
Url- Full URL (scheme://host:port)Host- Hostname or IPPort- Allocated port numberTargetPort- Container internal portScheme- Protocol (http, https, tcp)HostAndPort- Combined host:port
Named References
Provide custom names for service references:Service Discovery in Practice
Example: Calling Another Service
Here’s a complete example from the TestShop application:CatalogServiceClient.cs
Program.cs
AppHost Configuration
AppHost.cs
Run Mode vs. Publish Mode
Service discovery behaves differently in local development vs. deployed environments:Run Mode (Local Development)
- Endpoints resolved to
localhostwith dynamic ports - Environment variables contain concrete values
- Dashboard shows live endpoint information
Publish Mode (Deployment)
- Endpoints represented as expressions:
{catalogservice.bindings.http.url} - Deployment infrastructure resolves expressions at runtime
- Supports Kubernetes DNS, Azure service endpoints, etc.
Best Practices
Always use service defaults
Always use service defaults
Every service project should call
builder.AddServiceDefaults() to enable service discovery, telemetry, and health checks.Prefer service names over hardcoded URLs
Prefer service names over hardcoded URLs
Use service discovery URIs (
https+http://servicename) instead of hardcoding endpoints in configuration.Use named references for multiple instances
Use named references for multiple instances
When referencing multiple instances of the same resource type, provide distinct names to avoid conflicts.
Test endpoint resolution
Test endpoint resolution
Use the dashboard to verify that endpoints resolve correctly for your network topology.
Next Steps
Application Model
Learn about resources and the app model
Configuration Guide
Configure services and environment variables
Dashboard
Monitor service endpoints in the dashboard
Dependencies
Manage resource dependencies and startup order