Aspire provides a set of smart defaults for services that are commonly used in .NET applications. These defaults are designed to help you get started quickly and provide a consistent experience across different types of applications. This includes:
- Telemetry: Metrics, Tracing, Logging
- Resiliency
- Health Checks
- Service Discovery
-
Add a new project to the solution called
ServiceDefaults:- Right-click on the solution and select
Add>New Project. - Select the
Aspire Service Defaultsproject template. - Name the project
ServiceDefaults. - Click
Next>Create.
- Right-click on the solution and select
-
Create a new project using the
dotnet new aspire-servicedefaultscommand:dotnet new aspire-servicedefaults -n ServiceDefaults
Aspire CLI Tip: You can also use
aspire addfrom within your project to interactively add service defaults and other integrations. The CLI will guide you through selecting the right packages.
Before we dive into the mechanics, here’s why we do each of the next steps:
- Add project references: This lets the
ApiandMyWeatherHubprojects consume the shared configuration, resilience, telemetry, and discovery wiring that lives in the newServiceDefaultsproject without repeating that setup in every service. - Call
builder.AddServiceDefaults(): This opt‑in extension applies the opinionated “smart defaults” (OpenTelemetry logging/tracing/metrics, health checks, service discovery, and resilient HTTP handlers) so you start with production‑minded behavior immediately. - Call
app.MapDefaultEndpoints(): This maps development‑time health endpoints (/health,/alive) and prepares the app for later dashboard and orchestration scenarios. We’ll go deeper into these concepts in later modules; for now just know this lights up diagnostics and readiness probes early.
-
Add a reference to the
ServiceDefaultsproject in theApiandMyWeatherHubprojects:- Right-click on the
Apiproject and selectAdd>Reference.- Check the
ServiceDefaultsproject and clickOK.
- Check the
- Right-click on the
MyWeatherHubproject and selectAdd>Reference.- Check the
ServiceDefaultsproject and clickOK.
- Check the
Pro Tip: In Visual Studio 2026, you can drag and drop the project onto another project to add a reference.
- Right-click on the
-
In both the
ApiandMyWeatherHubprojects, update theirProgram.csfiles, adding the following line immediately after theirvar builder = WebApplication.CreateBuilder(args);line:builder.AddServiceDefaults();
-
In both the
ApiandMyWeatherHubprojects, update theirProgram.csfiles,adding the following line immediately after theirvar app = builder.Build();line:app.MapDefaultEndpoints();
-
Run the application using a multiple-project launch configuration in Visual Studio or Visual Studio Code:
- Visual Studio: Right click on the
MyWeatherHubsolution and go to properties. Select theApiandMyWeatherHubas startup projects, selectOK. - Visual Studio Code: Run the
ApiandMyWeatherHubprojects using theRun and Debugpanel. We have provided alaunch.jsonfile with the necessary configurations to run both.
- Visual Studio: Right click on the
-
Test the application by navigating to the following URLs:
- https://localhost:7032/openapi/v1.json - API OpenAPI document
- https://localhost:7274/ - MyWeatherHub
-
You should see the OpenAPI document (JSON) for the API and the MyWeatherHub home page.
-
You can also view the health checks for the API by navigating to https://localhost:7032/health.
-
You can also view the health checks for the MyWeatherHub by navigating to https://localhost:7274/health.
-
View the logs in the terminal to see the health checks and other telemetry data such as resiliency with Polly:
Polly: Information: Execution attempt. Source: '-standard//Standard-Retry', Operation Key: '', Result: '200', Handled: 'False', Attempt: '0', Execution Time: '13.0649'
-
Click on 5 different cities and a "random" error will be thrown. You will see the Polly retry policy in action.
Polly: Warning: Execution attempt. Source: '-standard//Standard-Retry', Operation Key: '', Result: '500', Handled: 'True', Attempt: '0', Execution Time: '9732.8258' Polly: Warning: Resilience event occurred. EventName: 'OnRetry', Source: '-standard//Standard-Retry', Operation Key: '', Result: '500' System.Net.Http.HttpClient.NwsManager.ClientHandler: Information: Sending HTTP request GET http://localhost:5271/forecast/AKZ318


