In the ever-evolving landscape of cloud-native application development, building scalable, observable, and production-ready distributed systems can be a challenging task. This is where .NET Aspire comes into play. .NET Aspire is an opinionated, cloud-ready stack designed to simplify the development of cloud-native applications by providing a collection of NuGet packages that handle specific cloud-native concerns.
Table of Contents
Understanding .NET Aspire
Cloud-native applications are characterized by their architecture, which often involves small, interconnected microservices consuming various services like databases, messaging, and caching. .NET Aspire addresses the challenges associated with building and managing such applications by offering solutions in three key areas:
1. Orchestration
.NET Aspire facilitates the orchestration of multiproject applications and their dependencies. Orchestration involves coordinating and managing the different elements within a cloud-native application. It streamlines the configuration and interconnection of various parts, providing useful abstractions for handling service discovery, environment variables, and container configurations.
The following example creates a local Redis container resource and configures the appropriate connection string in the frontend project with only two helper method calls:
// Example of adding a Redis container to the application
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedisContainer("cache");
builder.AddProject<Projects.MyFrontend>("frontend").WithReference(cache);
2. Components
.NET Aspire components are NuGet packages designed to simplify connections to popular services and platforms, such as Redis or PostgreSQL. These components handle cloud-native concerns through standardized configuration patterns, including health checks and telemetry.
The following example uses the .NET Aspire Service Bus component:
// Example of adding Azure Service Bus component
builder.AddAzureServiceBusClient("servicebus");
The .AddAzureServiceBusClient method handles the following:
- Registers a
ServiceBusClientas a singleton in the DI container for connecting to Azure Service Bus - Applied
ServiceBusClientconfigurations either inline through code or through configuration - Enables corresponding health checks, logging and telemetry specific to the Azure Service Bus usage
3. Tooling
.NET Aspire comes equipped with project templates and tooling experiences for both Visual Studio and the dotnet CLI, making it easier for developers to create and interact with .NET Aspire applications.
The Structure of .NET Aspire Apps
.NET Aspire applications follow a standardized structure that includes at least three projects:
- Starter App: Your initial app, which can be any common .NET project such as a Blazor UI or Minimal API.
- AppHost: Manages high-level orchestration concerns, including the configuration of various parts of your app.
- ServiceDefaults: Contains default .NET Aspire app configurations related to health checks, OpenTelemetry settings, and more.
Two starter templates are available to help you get started:
- .NET Aspire Application: A basic starter template providing essential components for further development.
- .NET Aspire Starter Application: This template includes boilerplate UI and API projects preconfigured with service discovery and other basic examples of common .NET Aspire functionality.
The .NET Aspire templates also include boilerplate extension methods to handle common service configurations:
builder.AddServiceDefaults();
Building Your First .NET Aspire App
To embark on your journey with .NET Aspire and build your first cloud-native application, follow these quickstart tasks. By the end, you’ll have a basic understanding of setting up a .NET Aspire app and orchestrating communication between different components.
Prerequisites
Before you begin, ensure you have the following tools and dependencies installed locally:
- .NET 8.0
- .NET Aspire workload (install using Visual Studio installer or
dotnet workload install aspirecommand) - Docker Desktop
- Integrated Developer Environment (IDE) or code editor (e.g., Visual Studio 2022 or Visual Studio Code)
For additional information, refer to the .NET Aspire setup and tooling documentation.
Task 1: Create a Basic .NET App
To create a new .NET Aspire Starter Application template solution, you can use either Visual Studio or the .NET CLI.
Visual Studio
- Open Visual Studio.
- Navigate to
File > New > Project. - In the dialog window, search for “Aspire” and select “.NET Aspire Starter Application.” Click
Next. - On the “Configure your new project” screen:
- Enter a Project Name of “AspireSample.”
- Leave the other values at their defaults and select
Next.
- On the “Additional information” screen:
- Ensure that “.NET 8.0 (Long Term Support)” is selected.
- Check “Use Redis for caching (requires Docker).”
- Click
Create.
Visual Studio will create a new solution structured to use .NET Aspire.
.NET CLI
dotnet new aspire -n AspireSample cd AspireSample
This command creates a new .NET Aspire Starter Application.
Task 2: Add and Configure .NET Aspire Component for Caching
In your application, you’ll configure Redis for caching using .NET Aspire components.
// In your Program.cs file
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedisContainer("cache");
builder.AddProject<Projects.MyFrontend>("frontend").WithReference(cache);
This code sets up a Redis container resource labeled “cache” and configures the frontend project to use it.
Task 3: Create an API and Use Service Discovery
Now, let’s add an API to your application and use service discovery to connect to it.
// In your Program.cs file
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedisContainer("cache");
var apiService = builder.AddProject<Projects.ApiService>("apiservice");
builder.AddProject<Projects.Frontend>("webfrontend").WithReference(cache).WithReference(apiService);
This code adds an API service project and configures the frontend project to use both the Redis cache and the API service.
Task 4: Orchestrate Communication Between Components
In this task, we’ll orchestrate communication between the frontend UI, the API, and the Redis cache.
// In your Program.cs file
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedisContainer("cache");
var apiService = builder.AddProject<Projects.ApiService>("apiservice");
builder.AddProject<Projects.Frontend>("webfrontend").WithReference(cache).WithReference(apiService);
builder.Build().Run();
This code builds and runs your .NET Aspire application, orchestrating communication between the different components.
Test Your App Locally
- Set the AspireSample.AppHost project as the startup project in Visual Studio.
- Press
F5to run the app. - Navigate from the home page to the weather page in the browser.
- Verify that the weather data is loaded, and periodically refresh the page to observe caching in action.
Congratulations! You have successfully completed the quickstart and build your first .NET Aspire application. Explore the provided dashboard to gain insights into your application’s various components and functionalities.
.
Original Article Source: Exploring .NET Aspire: Building Cloud-Native Apps with Ease written by Chris Pietschmann (If you're reading this somewhere other than Build5Nines.com, it was republished without permission.)

Stop Wasting Hours Writing Unit Tests: Use GitHub Copilot to Explode Code Coverage Fast
Microsoft Azure Regions: Interactive Map of Global Datacenters
Create Azure Architecture Diagrams with Microsoft Visio
IPv4 Address CIDR Range Reference and Calculator
Deploy Azure App Service / Web Apps with Deployment Slots for Reduced Downtime




