Working with .NET Aspire
Hey folks :)
Today I want to discuss .NET Aspire and how we can start playing with it.
What is .NET Aspire?
Microsoft announced .NET Aspire in the last .NET Conf as a stack for building observable, production-ready, distributed applications thought to improve the experience of building .NET cloud-native apps.
When I saw the announcement and the official article about .NET Aspire, I thought that this could help a lot of teams build applications faster and think in cloud-native solutions.
By default, .NET Aspire will help us with:
- Orchestration: This will help us with connecting multi-project applications and their dependencies.
- Components: This will help us to use NuGet packages for commonly used services, such as Redis, RabbitMQ, Postgres, etc.
- Tooling: This will help us with project templates and tooling experiences for Visual Studio and the dotnet CLI.
What is necessary to start?
To start playing with .NET Aspire, we need to install:
- .NET 8 SDK (https://dotnet.microsoft.com/en-us/download/dotnet/8.0)
- Docker Desktop (Only necessary if you want to create the project using containers. It is our scenario) https://docs.docker.com/desktop/install/windows-install/
- Visual Studio 2022 Preview (https://visualstudio.microsoft.com/vs/preview/)
During Visual Studio 2022 Preview Installer, we need to check the .NET Aspire SDK.
Creating the first .NET Aspire Project
To build our first project, we must open the Visual Studio 2022 Preview and follow the steps below.
1 — Create an application with .NET Aspire Starter Application
For now, we won’t use Redis, we’ll use the RabbitMQ.
2 — The project solution was created, let’s understand the projects.
- ApiService: This project is responsible for serving our API. This API will be requested by the Web project.
- AppHost: This project is responsible for the orchestration of our projects and dependencies.
- ServiceDefaults: This project is responsible for the management and configuration of our dependencies.
- Web: This project will be our front end. It is a Blazor project and will request our API.
3 — Executing the project
Let’s try to see our first .NET Aspire project and deep dive into this.
Please see that our startup project is the AppHost. This project will orchestrate our projects. Let’s see the program.cs
var builder = DistributedApplication.CreateBuilder(args);
var apiservice = builder.AddProject<Projects.AspireExample_ApiService>("apiservice");
builder.AddProject<Projects.AspireExample_Web>("webfrontend")
.WithReference(apiservice);
builder.Build().Run();
According to the code above we’re referencing our webfrontend project with our apiservice project. It only occurs because our AppHost project has project references with each other.
Press F5 to execute the project.
When the project starts we can see the .NET Aspire Dashboard.
By default, we now have Logs, Traces, and Metrics (Observability) about our projects and containers.
Just to generate some information, I will request our API.
http://localhost:5549/weatherforecast
Logs
When you click on Projects inside the Logs submenu, we can see the logs of our project
Metrics
When you click on the Metrics button, we can choose the project and see the metrics
Traces
When you click on the Traces button, we can see the requests made on our API and see some traces
Done!
Now, we already know how each project works and what .NET Aspire gives us by default. For the next article, we will integrate the RabbitMQ container with .NET Aspire project.
Thanks for reading!
Working with .NET Aspire — Step2