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.

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:

  1. Registers a ServiceBusClient as a singleton in the DI container for connecting to Azure Service Bus
  2. Applied ServiceBusClient configurations either inline through code or through configuration
  3. 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:

  1. Starter App: Your initial app, which can be any common .NET project such as a Blazor UI or Minimal API.
  2. AppHost: Manages high-level orchestration concerns, including the configuration of various parts of your app.
  3. 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:

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

  1. Open Visual Studio.
  2. Navigate to File > New > Project.
  3. In the dialog window, search for “Aspire” and select “.NET Aspire Starter Application.” Click Next.
  4. On the “Configure your new project” screen:
    • Enter a Project Name of “AspireSample.”
    • Leave the other values at their defaults and select Next.
  5. 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

  1. Set the AspireSample.AppHost project as the startup project in Visual Studio.
  2. Press F5 to run the app.
  3. Navigate from the home page to the weather page in the browser.
  4. 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.

.

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
Microsoft MVP HashiCorp Ambassador

Discover more from Build5Nines

Subscribe now to keep reading and get access to the full archive.

Continue reading