Core Concepts
The application model is built around three fundamental concepts:- Resources: The building blocks of your application (projects, containers, databases, etc.)
- Builders: Fluent APIs for configuring resources and their relationships
- References: Connections between resources that establish dependencies
What is a Resource?
In Aspire, a resource represents any component that makes up your distributed application. Resources are pure data objects that describe capabilities, configuration, and relationships—they don’t manage their own lifecycle. Resources implement theIResource interface and are identified by a unique name within the application graph:
Built-in Resource Types
Aspire provides several fundamental resource types:| Resource Type | Purpose | Example |
|---|---|---|
ProjectResource | .NET projects | AddProject<Projects.MyApi>("api") |
ContainerResource | Docker containers | AddContainer("redis", "redis") |
ExecutableResource | Arbitrary executables | AddExecutable("tool", "python") |
Fluent Configuration API
Resources are configured using a fluent API pattern. Extension methods likeAddRedis, AddPostgres, or AddProject encapsulate:
- Resource construction and registration
- Attachment of metadata (annotations)
- Default configuration
- Relationship wiring
Example: Complete AppHost Configuration
Here’s a real-world example from the TestShop playground application:Resource Annotations
Annotations are the primary extensibility mechanism in Aspire. They attach metadata to resources without modifying core classes. Annotations enable:- Service discovery configuration
- Environment variable injection
- Health check definitions
- Custom behaviors
Standard Interfaces
Resources declare capabilities through optional interfaces that enable dynamic wiring and orchestration:| Interface | Purpose | Example Usage |
|---|---|---|
IResourceWithEnvironment | Supports environment variables | .WithEnvironment("KEY", "value") |
IResourceWithServiceDiscovery | Enables service discovery | .WithReference(otherService) |
IResourceWithEndpoints | Exposes network endpoints | .WithEndpoint(port: 8080) |
IResourceWithConnectionString | Provides connection strings | Database resources |
IResourceWithWaitSupport | Can wait for dependencies | .WaitFor(otherResource) |
Interface Example
Resource References and Dependencies
TheWithReference method establishes dependencies between resources and wires configuration automatically:
- Connection string is automatically injected as environment variables
- Service discovery information is configured
- Startup ordering is established (database must be ready first)
Wait Dependencies
Use.WaitFor() to ensure a resource doesn’t start until its dependency is ready:
Environment Variables and Configuration
Aspire automatically manages environment variables for service discovery and configuration:Custom Environment Variables
Add custom configuration usingWithEnvironment:
Resource Lifecycle and State
Resources progress through well-defined states during execution:| State | Meaning |
|---|---|
Unknown | Default state when added to graph |
Starting | Resource is launching |
Running | Successfully started |
Waiting | Blocked waiting for dependencies |
Exited | Completed execution |
FailedToStart | Startup failed |
Parent-Child Relationships
Resources can have hierarchical relationships for lifecycle containment and dashboard visualization:- Start only after parent starts
- Stop automatically when parent stops
- Appear nested in the dashboard UI
Common Configuration Patterns
Data Persistence
External Access
Replicas and Scaling
Health Checks
Next Steps
Service Discovery
Learn how services find and communicate with each other
Dashboard
Explore the Aspire Dashboard for monitoring your app
Building Apps
Deep dive into building applications with Aspire
Resources Guide
Complete guide to working with resources