What is Aspire?
.NET Aspire is an opinionated, cloud-ready stack for building distributed applications. It provides:Service Orchestration
Manages containers, dependencies, and startup order
Service Discovery
Automatic connection string injection between services
Configuration
Environment variables wired automatically
Observability
Built-in OpenTelemetry tracing, metrics, and logs
AppHost Configuration
TheFSH.Playground.AppHost project defines the entire application topology. Here’s the actual configuration:
src/Playground/FSH.Playground.AppHost/AppHost.cs
How It Works
Builder Creation
Aspire creates a distributed application builder:This reads configuration from
appsettings.json and environment variables.PostgreSQL Container
Adds a PostgreSQL container with persistent storage:What this does:
- Pulls
postgres:17image from Docker Hub - Creates a named Docker volume
fsh-postgres-datafor persistence - Creates a database named
fsh - Generates a connection string automatically
- Exposes on default port
5432
Redis Container
Adds a Redis container for distributed caching:What this does:
- Pulls
redis:7image - Creates volume
fsh-redis-data - Exposes on default port
6379
Playground API Project
Registers the API project with service references and configuration:Key features:
.WithReference(postgres): Injects the PostgreSQL connection string as environment variable.WaitFor(postgres): Ensures Postgres is healthy before starting the API- Environment variables: Automatically wired from Aspire to the API
Environment Variables Injected
Aspire automatically injects these environment variables into the Playground API:| Variable | Value | Purpose |
|---|---|---|
ASPNETCORE_ENVIRONMENT | Development | Enables development mode |
DatabaseOptions__Provider | POSTGRESQL | Sets EF Core provider |
DatabaseOptions__ConnectionString | {postgres.connectionString} | Auto-generated connection string |
DatabaseOptions__MigrationsAssembly | FSH.Playground.Migrations.PostgreSQL | Migrations project |
CachingOptions__Redis | {redis.connectionString} | Redis connection string |
OpenTelemetryOptions__Exporter__Otlp__Endpoint | https://localhost:4317 | OTLP exporter endpoint |
OpenTelemetryOptions__Exporter__Otlp__Enabled | true | Enables telemetry export |
Connection strings use Aspire’s
ConnectionStringExpression, which resolves at runtime to the actual container IP and port.Service Dependencies
Aspire ensures services start in the correct order: The.WaitFor() method ensures health checks pass before dependent services start.
AppHost Project Structure
src/Playground/FSH.Playground.AppHost/FSH.Playground.AppHost.csproj
Aspire.Hosting.PostgreSQL(v13.1.0): PostgreSQL container hostingAspire.Hosting.Redis(v13.1.0): Redis container hosting
Running the AppHost
Start the entire stack:Container Orchestration
Aspire pulls Docker images and starts containers:You’ll see
postgres and redis containers running.Health Checks
Aspire waits for container health checks before proceeding:
- PostgreSQL: Waits for TCP connection on port 5432
- Redis: Waits for PING response
Migration Execution
The Playground API calls
UseHeroMultiTenantDatabases() which:- Applies pending EF Core migrations
- Seeds initial data (admin user, permissions)
- Creates tenant schemas
Observability
Aspire enables comprehensive observability out of the box.OpenTelemetry Export
The API exports telemetry tohttp://localhost:4317 (OTLP gRPC endpoint):
From appsettings.json
Traced Operations
- HTTP requests: ASP.NET Core instrumentation
- EF Core queries: Database command tracing (filtered via
FilterEfStatements) - Redis commands: StackExchange.Redis instrumentation (filtered)
- Hangfire jobs: Background job execution
- Mediator handlers: Command/query handling
Metrics Collected
- Request duration histograms
- Module-specific meters (Identity, Multitenancy, Auditing)
- Job execution metrics
- Cache hit/miss rates
Customizing AppHost
Adding a New Service
To add another project or container:Using SQL Server Instead
Replace PostgreSQL with SQL Server:Adding Jaeger for Tracing
Data Persistence
Aspire creates named Docker volumes for data persistence:View volumes
Aspire Dashboard
Aspire provides a web dashboard athttp://localhost:15000 (may vary) showing:
- Running services and their health
- Logs from all containers
- Distributed traces
- Metrics and histograms
- Environment variables
The dashboard URL is printed to the console when you run
dotnet run --project FSH.Playground.AppHost.Production Deployment
See Deployment Guide for production patterns.Next Steps
Configuration
Explore all configuration options
Observability
Deep dive into OpenTelemetry setup
Database
Learn about EF Core and migrations
Modules
Understand the modular architecture
