Aspire includes functionality for configuring service discovery at development and testing time. This allows resources to refer to other resources by their name and have the concrete address details resolved at runtime. Service discovery functionality works by providing configuration in the format expected by the configuration-based endpoint resolver from the Aspire App Host project to the individual service projects added to the application model.
Currently, the MyWeatherHub is using static configuration in its appsettings.json file to connect to the Api. This is not ideal for several reasons including:
- The port number of the
Apiservice may change. - The IP address of the
Apiservice may change. - Multiple configuration settings would need to be defined for http and https settings.
- As we add more services, the configuration would become more complex.
To address these issues, we will use the service discovery functionality provided by the Aspire App Host project. This will allow the MyWeatherHub service to discover the Api service at runtime.
-
Open the
Program.csfile in theAppHostproject. -
Earlier we added orchestration to include several projects by using the
builder.AddProjectmethod. This returned anIResourceBuilder<TResource>that can be used to reference other resources. Let's reference theApiproject in theMyWeatherHubproject by updating the code:var api = builder.AddProject<Projects.Api>("api"); var web = builder.AddProject<Projects.MyWeatherHub>("myweatherhub") .WithReference(api) .WithExternalHttpEndpoints();
-
The
WithReferencemethod is used to reference theApiproject. This will inject the required configuration to allow theMyWeatherHubproject to discover theApiproject at runtime via its resource name,api. -
If you later choose to deploy this app, you'd need the call to
WithExternalHttpEndpointsto ensure that it's public to the outside world.
When we added ServiceDefaults to the projects we automatically enrolled them in the service discovery system. This means that the MyWeatherHub project is already configured to use service discovery.
Some services expose multiple, named endpoints. By default, the scheme portion of the URI is used to refer to the name of the endpoint being resolved, e.g. the URI https://basket will resolve an endpoint named https on the basket service. By default in Aspire, project resources declare their endpoints according to the contents of their launchSettings.json file, so most projects will by default receive https and http named endpoints. The scheme portion of the referring URI can include multiple names separated by a + character and in preference order, e.g. https+http://basket will attempt to resolve the https named endpoint, and if not found it will resolve the http endpoint, of the basket service.
Note
This workshop uses implicit service discovery with the default http and https named endpoints. Aspire also supports explicitly declaring and resolving additional named endpoints (via code, configuration, and Kubernetes) for advanced scenarios. See the Named Endpoints section of the Service Discovery docs
for details.
Now, let's update the MyWeatherHub project to use service discovery to connect to the Api service. This can be accomplished by updating the existing WeatherEndpoint configuration settings in the appsettings.json. This is convenient when enabling Aspire in an existing deployed application as you can continue to use your existing configuration settings.
-
Open the
appsettings.jsonfile in theMyWeatherHubproject. -
Update the
WeatherEndpointconfiguration settings to use service discovery:"WeatherEndpoint": "https+http://api"
Multi-scheme service discovery: The value
https+http://apitells the resolver to prefer an HTTPS endpoint for theapiservice if one exists, and fall back to HTTP otherwise. The schemes are evaluated left-to-right and separated by+. This lets the same configuration work for local (HTTP only) development and future HTTPS-enabled environments without further changes. Use this pattern only for internal service-to-serviceHttpClientbase addresses—don’t surface the multi-scheme form directly in user-facing links. -
The
WeatherEndpointconfiguration setting is now using service discovery to connect to theApiservice.
Alternatively, we can update the url to not use the WeatherEndpoint configuration settings and instead set the URI directly in the NwsManager class:
-
Open the
Program.csfile in theMyWeatherHubproject. -
Update the
WeatherEndpointconfiguration settings to use service discovery:builder.Services.AddHttpClient<NwsManager>( static client => client.BaseAddress = new("https+http://api"));
-
Run the application by pressing
F5or selecting theStart Debuggingoption. -
Open the
MyWeatherAppby selecting the endpoint in the dashboard. -
Notice that the
MyWeatherHubapp still works and is now using service discovery to connect to theApiservice. -
In the dashboard click on the
Detailsfor theMyWeatherHubproject. This will bring up all of the settings that Aspire configured when running the app from the App Host. -
Click on the eye icon to reveal the values and scroll to the bottom where you will see
services__api__http__0andservices__api__https__0configured with the correct values of theApiservice.`
Aspire provides first-class support for modeling external services in your application graph. Modern applications frequently need to integrate with external APIs, third-party services, or existing infrastructure that isn't managed by Aspire.
External services can be added to your AppHost just like internal services, and they appear in the Aspire dashboard with health status monitoring. Let's add an external service reference for the National Weather Service's endpoint:
var builder = DistributedApplication.CreateBuilder(args);
// Reference an external API service
var weatherApi = builder.AddExternalService("weather-api", "https://api.weather.gov");
...
// Your services can reference external services just like internal ones
var api = builder.AddProject<Projects.Api>("api")
.WithReference(weatherApi);- Unified View: All services (internal and external) appear in the Aspire dashboard
- Health Monitoring: External services can include health checks
- Service Discovery: External services work with the same service discovery patterns
- Configuration Management: Centralized configuration for external dependencies
- Deployment Awareness: External services are included in deployment manifests
This feature bridges the gap between your Aspire-managed services and the broader ecosystem of services your application depends on.
Note: External service modeling is a development-time modeling feature. It doesn't proxy traffic—the clients still call the real external service endpoint; the AppHost provides discovery and visibility.
Now that we've defined the external weather API service in our AppHost, we need to update the API project to use service discovery to connect to it. The Api project already has an NwsManager class that makes HTTP requests to the National Weather Service.
-
Open the
NwsManager.csfile in theApi/Datafolder. -
Locate the
AddNwsManagerextension method around line 130. -
Update the
HttpClientto usehttps://weather-apias the base address (no trailing slash) instead of api.weather.gov, and ensure the request path starts with a leading slash:services.AddHttpClient<Api.NwsManager>(client => { client.BaseAddress = new Uri("https://weather-api"); client.DefaultRequestHeaders.Add("User-Agent", "Microsoft - Aspire Demo"); }); ... var zoneIdSegment = HttpUtility.UrlEncode(zoneId); var zoneUrl = $"/zones/forecast/{zoneIdSegment}/forecast"; var forecasts = await httpClient.GetFromJsonAsync<ForecastResponse>(zoneUrl, options);
The service discovery system will now resolve https://weather-api to the actual URL of the National Weather Service API (https://api.weather.gov) that we defined in the AppHost configuration.
This was just the start of what we can do with service discovery and Aspire. As our application grows and we add more services, we can continue to use service discovery to connect services at runtime. This will allow us to easily scale our application and make it more resilient to changes in the environment.
You can learn more about advanced usage and configuration of service discovery in the Aspire Service Discovery documentation.
Next: Module #5: Integrations

