Have you tried .NET Aspire?

Diego Pereira
5 min readDec 31, 2024

--

What is .NET Aspire, and how can it be used? In this article, I will show you how I implemented a file-processing workflow application using .NET 9 and .NET Aspire with integrations with Azure Services, Databases, and Cache.

.NET Aspire Overview

What is .NET Aspire?

.NET Aspire is a comprehensive suite of tools, templates, and NuGet packages designed to streamline the development of cloud-native, production-ready, and observable applications. It facilitates the creation of microservices and integration with various services like databases, messaging, and caching, catering to the needs of modern, interconnected app architectures.

Why .NET Aspire?

.NET Aspire enhances the development experience for .NET cloud-native applications by offering consistent tools and patterns for building and running distributed apps. It includes features for orchestration of multi-project applications, standardized NuGet integrations for services like Redis and Postgres, and comprehensive tooling for Visual Studio, Visual Studio Code, and the .NET CLI.

Orchestration

.NET Aspire’s orchestration simplifies local development by managing cloud-native app configurations and interconnections. While not a replacement for production tools like Kubernetes, it provides abstractions for service discovery, environment variables, and container setups, ensuring consistent patterns and easing the management of complex applications during development.

Integrations

.NET Aspire integrations are NuGet packages that simplify connecting to services like Redis or PostgreSQL by handling cloud-native concerns such as health checks and telemetry. Each integration includes a hosting package for the service and a corresponding client package for the consumer, working seamlessly with Aspire’s orchestration to automatically configure communication between services based on their resource references.

Service Discovery

.NET Aspire includes functionality for configuring service discovery at development and testing time. Service discovery functionality works by providing configuration in the format expected by the configuration-based endpoint resolver from the .NET Aspire AppHost project to the individual service projects added to the application model.

Service Defaults

The .NET Aspire Service Defaults project offers a collection of extension methods designed to integrate telemetry, health checks, and service discovery into applications. It is highly customizable and extensible, making it ideal for cloud-native applications that require robust configuration management.

It should not include other shared functionality or models, which should instead be managed in a conventional shared class library project.

Dashboard

.NET Aspire project templates include a robust dashboard for real-time app monitoring and inspection, also available in standalone mode. The dashboard offers insights into logs, traces, and environment configurations while allowing you to manage resources by stopping, starting, or restarting them, enhancing the development experience.

Deployments

.NET Aspire projects are designed with cloud-agnostic principles, offering deployment flexibility across various platforms that support .NET and containers. The provided guidelines can be adapted for deployment on different cloud environments or local hosting. While manual deployment is possible, it involves complex steps that are prone to errors. Users typically prefer using CI/CD pipelines and cloud-specific tools for a more efficient and error-free deployment tailored to their chosen infrastructure.

  • Manifest: To help deployment tools from Microsoft and other cloud providers understand the structure of .NET Aspire projects, specialized targets within the AppHost project can be executed to generate a manifest file. This file describes the projects and services used by the app, along with the necessary deployment properties, such as environment variables.
  • Azure: .NET Aspire projects are built to run in containerized environments. Azure Container Apps (ACA) is a fully managed, serverless platform that allows you to run microservices and containerized applications. The Azure Container Apps documentation provides guidance on deploying Aspire apps to ACA manually, using Bicep, or through the Azure Developer CLI (azd).
  • Kubernetes: Kubernetes is a widely used container orchestration platform capable of running .NET Aspire projects. To deploy these projects to Kubernetes clusters, the .NET Aspire JSON manifest must be mapped to a Kubernetes YAML manifest file. This can be done either by using the Aspir8 project or by manually creating the Kubernetes manifests.

Exploring the Code

Introduction

In this section, we will explore the core functionality of this project, which automates a file-processing workflow based on background services.

The first background service is responsible for:

  • Collecting files from a specified directory.
  • Calling an internal API to upload the file to Azure Blob Storage.
  • Sending a message to Azure Service Bus, and triggering a second background service.

The second background service is responsible for:

  • Reading the file and saving the data into MongoDB.
  • Sending a message to a third background service.

The third background service is responsible for:

  • Retrieving aggregated data from MongoDB.
  • Storing data in Postgres.
  • Caching the results in Redis.

AppHost Project

Program.cs is where we configure all of our application’s dependencies.

On line 74/75, we are defining all the dependencies for the API project and waiting for a “ready” state for our application to start.

The AppHost must have the dependency reference of the API Project.

.NET Built-in packages:

  • Aspire.Hosting.Azure.Storage
  • Aspire.Hosting.MongoDB
  • Aspire.Hosting.PostgreSQL
  • Aspire.Hosting.Redis
  • Aspire.Hosting.SqlServer

Note, that we have built-in integrations and custom integrations. The ServiceBus emulator is a docker image that needs the SQL Server dependency and Config.json file to work.

API Project (Client integrations)

The names of the built-in packages are quite similar:

  • Aspire.Azure.Messaging.ServiceBus
  • Aspire.Azure.Storage.Blobs
  • Aspire.MongoDB.Driver
  • Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
  • Aspire.StackExchange.Redis
  • Azure.Messaging.ServiceBus

Even using the ServiceBus simulator, we can use the built-in .NET Aspire package.

Ok, but how does my API know the dependencies set up on the AppHost?

ProgramExtensions.cs

I chose to divide it into two separate methods:

  • The first method registers the application dependencies
  • While the second method registers the service dependencies.

Program.cs

I have a list of services that use client integrations, and it’s the same way we used it even before .NET Apire.

Running

The AppHost project should be the Startup project.

The .NET Aspire dashboard should open and you can explore it.

I hope you enjoyed this article! Check out the full implementation of this application on GitHub. Feel free to contribute to the project.

See you in the next article.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Diego Pereira
Diego Pereira

Written by Diego Pereira

Sr. Software Developer at @Farfetch | Football fan | Beer lover | Coffee drinker

No responses yet

Write a response