Why .NET Aspire is a Game-Changer for Cloud-Native Development in .NET 9
.NET Aspire, introduced in .NET 8, is a comprehensive set of tools, templates, and NuGet packages designed to simplify cloud-native application development. By addressing key cloud-native concerns like orchestration, integrations, and tooling, .NET Aspire empowers developers to build scalable, observable, and production-ready apps. It enhances local development with streamlined orchestration features, simplifies connections to popular services like Redis and PostgreSQL through standardized integrations, and provides opinionated project templates with built-in defaults for telemetry, health checks, and service discovery. With its focus on scalability, resilience, and seamless cloud service connections, .NET Aspire ensures an optimized experience for developing distributed applications in the modern cloud ecosystem.
Prerequisites
To work with .NET Aspire, you need the following installed locally:
· .NET 9.0
· Docker Desktop
· .NET Aspire Workload
· Visual Studio 2022 version 17.9 or higher
We will install the .NET Aspire workload from the CLI:
To check current aspire version
dotnet workload list
To install the .net aspire workload
dotnet workload install aspire
To make sure latest .net aspire installed
dotnet workload update
We need to restart the machine to get the .NET Aspire project templates.
Create a .Net Aspire Application
- Let’s open the visual studio after restart and select create new project.
- In the dialog window, search for aspire and select .NET Aspire Starter Application from the project template and click next.
3. We will input a solution name as AspireApp.
4. Make sure .NET 9.0 (Standard Term Support) is selected.
5. Ensure that Use Redis for caching (requires Docker) is checked and select Create.
Understand the .NET Aspire solution structure
Our .NET Aspire project was created successfully. Let’s explore the projects and structure of the solution.
The solution is composed of the following projects:
- AspireApp.ApiService: This is an ASP.NET Core Minimal API project that supplies data to the frontend. It relies on the shared AspireApp.ServiceDefaults project for its functionality.
- AspireApp.AppHost: Serving as an orchestrator project, this component is responsible for connecting and configuring various projects and services in the application. It should be set as the Startup project and depends on both the AspireApp.ApiService and AspireApp.Web projects.
- AspireApp.ServiceDefaults: A shared .NET Aspire project that centralizes configurations for resilience, service discovery, and telemetry, which are utilized across all projects in the solution.
- AspireApp.Web: This is an ASP.NET Core Blazor application that uses the standard .NET Aspire service configurations. It has a dependency on the AspireApp.ServiceDefaults project.
Your AspireApp directory should resemble the following structure:
└───📂 AspireApp
├───📂 AspireApp.ApiService
│ ├───📂 Properties
│ │ └─── launchSettings.json
│ ├─── appsettings.Development.json
│ ├─── appsettings.json
│ ├─── AspireApp.ApiService.csproj
│ └─── Program.cs
├───📂 AspireApp.AppHost
│ ├───📂 Properties
│ │ └─── launchSettings.json
│ ├─── appsettings.Development.json
│ ├─── appsettings.json
│ ├─── AspireApp.AppHost.csproj
│ └─── Program.cs
├───📂 AspireApp.ServiceDefaults
│ ├─── AspireApp.ServiceDefaults.csproj
│ └─── Extensions.cs
├───📂 AspireApp.Web
│ ├───📂 Components
│ │ ├───📂 Layout
│ │ │ ├─── MainLayout.razor
│ │ │ ├─── MainLayout.razor.css
│ │ │ ├─── NavMenu.razor
│ │ │ └─── NavMenu.razor.css
│ │ ├───📂 Pages
│ │ │ ├─── Counter.razor
│ │ │ ├─── Error.razor
│ │ │ ├─── Home.razor
│ │ │ └─── Weather.razor
│ │ ├─── _Imports.razor
│ │ ├─── App.razor
│ │ └─── Routes.razor
│ ├───📂 Properties
│ │ └─── launchSettings.json
│ ├───📂 wwwroot
│ │ ├───📂 bootstrap
│ │ │ ├─── bootstrap.min.css
│ │ │ └─── bootstrap.min.css.map
│ │ ├─── app.css
│ │ └─── favicon.png
│ ├─── appsettings.Development.json
│ ├─── appsettings.json
│ ├─── AspireApp.Web.csproj
│ ├─── Program.cs
│ └─── WeatherApiClient.cs
└─── AspireApp.sln
Test the App Locally
The sample application consists of a Blazor frontend that interacts with a Minimal API project. The API provides mock weather data to the frontend. Service discovery is used by the frontend app to connect to the API project, while the API is configured with Redis-based output caching. The sample app is now prepared for testing, and the following conditions need to be verified:
· The weather page retrieves and displays weather data from the API project using service discovery.
· Subsequent requests are efficiently served through output caching enabled by the .NET Aspire Redis integration.
To test the app, open Visual Studio and set the AspireApp.AppHost project as the startup project by right-clicking it in Solution Explorer and selecting Set as Startup Project. This might already be pre-configured. After that, press F5 to run the app with debugging or Ctrl + F5 to run it without debugging.
- The app displays the .NET Aspire dashboard in the browser. The dashboard will show two running applications: apiservice and webfrontend.
2. Let’s open webfrontend application by selecting the project’s localhost endpoint.
The home page of the webfrontend app displays “Hello, world!”
3. Use the left-side navigation to move from the home page to the weather page. The weather page presents weather data in a forecast table. Take note of some of the values displayed in the table.
4. Refresh the page periodically over a span of 10 seconds. During this time, the cached data will be displayed. After 10 seconds, a new set of weather data will appear, as the data is randomly generated and the cache gets refreshed.
👏 Congratulations! We successfully run the .NET Aspire application.
Furthermore, you can see in our desktop docker that a docker image is created and the container is running.
This is how we can create .NET Aspire, a cloud-ready solution for building observable, production-ready, distributed applications.
Monitoring with .NET Aspire Dashboard
When you run a AppHost project, a dashboard launches that you use to monitor various parts of your app.
- Resources
Lists basic information for all of the individual .NET projects in your .NET Aspire project, such as the app state, endpoint addresses, and the environment variables that were loaded in.
2. Console
Displays the console output from each of the projects in your app.
3. Structured
Displays structured logs in table format. These logs support basic filtering, free-form search, and log level filtering as well. You should see logs from the apiservice
and the webfrontend
. You can expand the details of each log entry by selecting the View button on the right end of the row.
4. Traces
Displays the traces for your application, which can track request paths through your apps. Locate a request for /weather and select View on the right side of the page. The dashboard should display the request in stages as it travels through the different parts of your app.
5. Metrics
Displays various instruments and meters that are exposed and their corresponding dimensions for your app. Metrics conditionally expose filters based on their available dimensions.
For further insights and a comprehensive understanding, refer to Explore the .NET Aspire dashboard, and What Is .NET Aspire. These resources will provide additional information and practical examples to enhance your learning about .Net Aspire.
Happy Learning! Cheers!