.NET Aspire includes functionality for configuring service discovery at development and testing time. Service discovery functionality works by providing configuration in the format expected by the configuration-based endpoint resolver from the .NET Aspire AppHost project to the individual service projects added to the application model.
Currently, the MyWeatherHub is using a static configuration 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 .NET Aspire AppHost 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 anIResourceBuildthat can be used to reference projects. 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 allow theMyWeatherHubproject to discover theApiproject at runtime. -
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. Named endpoints can be resolved by specifying the endpoint name in the host portion of the HTTP request URI, following the format scheme://_endpointName.serviceName. For example, if a service named "basket" exposes an endpoint named "dashboard", then the URI scheme+http://_dashboard.basket can be used to specify this endpoint, for example:
builder.Services.AddHttpClient<BasketServiceClient>(
static client => client.BaseAddress = new("https+http://basket"));
builder.Services.AddHttpClient<BasketServiceDashboardClient>(
static client => client.BaseAddress = new("https+http://_dashboard.basket"));In the above example, the BasketServiceClient will use the default endpoint of the basket service, while the BasketServiceDashboardClient will use the dashboard endpoint of the basket service. 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 .NET 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"
-
The
WeatherEndpointconfiguration setting is now using service discovery to connect to theApiservice.
Optionally, we can update the url to not use the WeatherEndpoint configuration settings.
-
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
MyWeatheAppby 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 .NET 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.`
This was just the start of what we can do with service discovery and .NET 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 .NET Aspire Service Discovery documentation.
