.NET 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
.NET Aspire Service Defaults
project template. - Name the project
ServiceDefaults
. - Click
Next
>Create
.
- Right-click on the solution and select
-
Create a new project using the
dotnet new aspire-servicedefaults
command:dotnet new aspire-servicedefaults -n ServiceDefaults
-
Add a reference to the
ServiceDefaults
project in theApi
andMyWeatherHub
projects:- Right-click on the
Api
project and selectAdd
>Reference
.- Check the
ServiceDefaults
project and clickOK
.
- Check the
- Right-click on the
MyWeatherHub
project and selectAdd
>Reference
.- Check the
ServiceDefaults
project and clickOK
.
- Check the
Pro Tip: In Visual Studio 2022, you can drag and drop the project onto another project to add a reference.
- Right-click on the
-
In both the
Api
andMyWeatherHub
projects, update theirProgram.cs
files, adding the following line immediately after theirvar builder = WebApplication.CreateBuilder(args);
line:builder.AddServiceDefaults();
-
In both the
Api
andMyWeatherHub
projects, update theirProgram.cs
files,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
MyWeatherHub
solution and go to properties. Select theApi
andMyWeatherHub
as startup projects, selectOK
. - Visual Studio Code: Run the
Api
andMyWeatherHub
projects using theRun and Debug
panel. We have provided alaunch.json
file 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/swagger/index.html - API
- https://localhost:7274/ - MyWeatherHub
-
You should see the Swagger UI 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