Introducing Aspire
What is Aspire?
Aspire is an opinionated, cloud-ready stack for building observable, production-ready, distributed applications. It provides tools, templates, and packages to simplify the development of .NET cloud-native apps.
At the center of Aspire is the app model—a code-first, single source of truth that defines your app's services, resources, and connections. Aspire gives you a unified toolchain: launch and debug your entire app locally with one command, then deploy anywhere—Kubernetes, the cloud, or your own servers—using the same composition.
Key Components
-
AppHost (Orchestration): Define your services, dependencies, and configuration in code. The AppHost project orchestrates running and connecting multi-project applications and their dependencies for local development environments.
-
Rich Integrations: NuGet packages for popular services with standardized interfaces. Aspire integrations simplify connections to databases, messaging systems, caching, and more (like Redis, PostgreSQL, RabbitMQ, Azure services). Each integration provides standardized patterns for health checks, telemetry, and configuration.
-
Developer Dashboard: A powerful web-based dashboard that gives you real-time visibility into your distributed app. Inspect resources, view logs, traces, and metrics, and manage your app's services—all from a single UI. The dashboard launches automatically when you run your Aspire app.
-
Consistent Tooling: Project templates and experiences for Visual Studio, Visual Studio Code, and the dotnet CLI to help you create and interact with Aspire apps efficiently.
Prerequisites
Before adding Aspire to your project, ensure you have the following:
- .NET SDK 9.0 or later - Aspire requires the latest .NET SDK
- Container runtime - Docker Desktop or Podman. Aspire projects are designed to run in containers.
- Aspire templates - Install using the command below
Installing Aspire Templates
With Aspire 9 and later, the Aspire workload is no longer required. Instead, you only need to install the project templates.
To install the Aspire templates, run:
dotnet new install Aspire.ProjectTemplates
To verify that the templates are installed, run:
dotnet new list aspire
You should see templates like aspire, aspire-apphost, aspire-servicedefaults, and others in the output.
Adding Aspire
Now we need to introduce the two main Aspire projects in our solution.
First, cd into the src folder of our solution and create the Aspire App Host by running:
dotnet new aspire-apphost -o Dometrain.Aspire.AppHost
This will create a new project called Dometrain.Aspire.AppHost.
We will also need a ServiceDefaults project. To create that run:
dotnet new aspire-servicedefaults -o Dometrain.Aspire.ServiceDefaults
We can now add the two projects to our solution.
Let's take a look at the two projects
Dometrain.Aspire.AppHost
The Aspire AppHost project is the orchestration layer for your distributed application. It serves as the code-first, single source of truth that defines your app's services, resources, and connections.
Key responsibilities:
- Local Development: Runs and orchestrates your entire system locally, including APIs, background services, databases, caches, and other infrastructure
- Service Discovery: Ensures all your services can communicate with each other properly
- Deployment: The same app model can be used to deploy your application anywhere—Kubernetes, Azure, or your own servers
As of now it only has the following 2 lines of code, but we will be adding many more:
var builder = DistributedApplication.CreateBuilder(args);
builder.Build().Run();
When you run the AppHost, the Aspire Dashboard will launch automatically, giving you real-time visibility into logs, traces, and metrics for all services.
Dometrain.Aspire.ServiceDefaults
The Aspire Service Defaults project is a shared library that configures common concerns for all services in your application:
- Telemetry: Automatic logging, metrics, and distributed tracing
- Health Checks: Standardized health check endpoints
- Service Discovery: Enables services to find and communicate with each other
- Resilience: Built-in retry policies and circuit breakers
This project is referenced by your service projects, and the AddServiceDefaults() and MapDefaultEndpoints() extension methods handle all the configuration automatically.
Wiring Aspire up
To wire Aspire up, first we need to refer Dometrain.Aspire.ServiceDefaults to the main API project.
Once that's done we can add the AddServiceDefaults call in the Program.cs:
builder.AddServiceDefaults();
Then we can add the Aspire endpoints by calling the MapDefaultEndpoints method under the Swagger calls:
app.MapDefaultEndpoints();
With all that now done we can wire up the API project to run through Aspire's AppHost project.
To do that we need the following inside AppHost's Program.cs:
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.Dometrain_Monolith_Api>("dometrain-api");
builder.Build().Run();
This will enable us to run the Dometrain Api when the AppHost is started.
Since we will be running this project using http we will also need the following environment variable inside the launch profile:
"ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true"
Now we can finally run the Aspire AppHost!