Working with .NET Aspire

Cesar Augusto
4 min readJan 3, 2024

--

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:

During Visual Studio 2022 Preview Installer, we need to check the .NET Aspire SDK.

After clicking on the Install button, wait until the installation is done.

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.

Click on the Create Button to create our project solution

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

Request duration according to the filters

Traces

When you click on the Traces button, we can see the requests made on our API and see some traces

Click on the View button

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

--

--

Cesar Augusto

Software & Solutions Architect | Software Engineer | 6X Microsoft Azure Certified | 4X AWS Certified