Aspire.Hosting library to describe your application’s resources and their relationships.
Understanding the AppHost
The AppHost is the orchestrator for your .NET Aspire application. It’s a console application that:- Defines all resources (projects, containers, databases, etc.)
- Describes dependencies between resources
- Configures service discovery
- Manages the application lifecycle
Basic AppHost Structure
Every AppHost follows this basic structure:Program.cs
The
DistributedApplication.CreateBuilder() method creates an instance of IDistributedApplicationBuilder, which provides the foundation for defining your application:Why pass
args? The command-line arguments are essential for deployment tools to communicate with the AppHost. Always pass args from your Main method or top-level statements.// Add a Redis container
var cache = builder.AddRedis("cache");
// Add a PostgreSQL server with a database
var db = builder.AddPostgres("postgres")
.AddDatabase("catalogdb");
// Add a .NET project
var api = builder.AddProject<Projects.Api>("api");
Each
Add* method returns an IResourceBuilder<T> that you can use to further configure the resource.Builder Configuration Options
You can customize the builder’s behavior usingDistributedApplicationOptions:
Common Options
DisableDashboard
DisableDashboard
Disables the Aspire dashboard. Useful for headless scenarios or CI/CD environments.
AllowUnsecuredTransport
AllowUnsecuredTransport
Allows unsecured HTTP communication with the dashboard. Only use for development.
ContainerRegistryOverride
ContainerRegistryOverride
Forces all container images to be pulled from a specific registry.
Accessing Services
TheIDistributedApplicationBuilder exposes properties for customizing the application:
Execution Modes
The AppHost can run in different modes depending on how it’s invoked:Run Mode
Normal execution mode where the AppHost starts all resources and the dashboard:Publish Mode
Generates deployment manifests for publishing to cloud environments:Resource Lifecycle
TheDistributedApplication implements the standard .NET IHost interface, which means it follows the host lifecycle:
Monitoring Resource State
You can monitor and react to resource state changes:AppHost Project Structure
A typical AppHost project has this structure:Project File Requirements
The AppHost project file must:- Target
net10.0or later - Set
IsAspireHosttotrue - Reference the
Aspire.Hosting.AppHostpackage
MyApp.AppHost.csproj
Next Steps
Now that you understand the basics of the AppHost, learn how to:Define Resources
Learn about containers, projects, and executables
Wire Dependencies
Connect resources using WithReference
Manage Configuration
Configure your resources and services
Set Environment Variables
Pass configuration to your resources