What is the AppHost?
Aspire’s AppHost is where you define your app’s services and dependencies in code—no complex configs required. Easily map out your architecture and let Aspire handle the local orchestration, so you can focus on building features.
Defining Your Architecture
Section titled “Defining Your Architecture”Consider a three-tier architecture where your frontend talks to an API, and your API connects to a database:
architecture-beta service db(logos:postgresql)[PostgreSQL] service api(logos:dotnet)[API Service] service frontend(logos:react)[Frontend] api:R --> L:db frontend:R --> L:api
This could be represented in the AppHost
as shown in the following code:
var builder = DistributedApplication.CreateBuilder(args);
// Add database service to orbitvar postgres = builder.AddPostgres("db") .AddDatabase("appdata") .WithDataVolume();
// Add API service and reference the databasevar api = builder.AddProject<Projects.Api>("api") .WithReference(postgres) .WaitFor(postgres);
// Add frontend service and reference the APIvar frontend = builder.AddNpmApp("frontend", "../frontend") .WithHttpEndpoint(env: "PORT", targetPort: 3000) .WithNpmPackageInstallation() .WithReference(api);
builder.Build().Run();
Regardless of the language you choose, Aspire provides a consistent way to define your app’s architecture. You can easily add services, set up dependencies, and configure how they interact—all in a straightforward, code-first manner.
How the AppHost Works
Section titled “How the AppHost Works”The AppHost project serves as the orchestration layer for your distributed application. When you run the AppHost:
- Service Discovery: Aspire automatically discovers all the services and resources defined in your AppHost
- Dependency Resolution: Services are started in the correct order based on their dependencies
- Configuration Injection: Connection strings and service endpoints are automatically injected into your services
- Health Monitoring: Aspire monitors the health of all services and can restart them if needed
Dive deeper into Aspire’s orchestration and discover how it manages your resources in the AppHost’s Resource Model.
AppHost Project Structure
Section titled “AppHost Project Structure”The template AppHost project contains:
DirectoryAspireApp.AppHost
DirectoryProperties
- launchSettings.json
- appsettings.Development.json
- appsettings.json
- AspireApp.AppHost.csproj
- AppHost.cs
AppHost Lifecycle Events
Section titled “AppHost Lifecycle Events”The AppHost provides several lifecycle events that you can subscribe to for custom behavior during the app’s startup process. These events allow you to hook into key moments in the lifecycle of your application, such as before the app starts or after resources are created.
BeforeStartEvent
: Raised before the AppHost starts.AfterEndpointsAllocatedEvent
: Raised after the AppHost has allocated endpoints for all services.AfterResourcesCreatedEvent
: Raised after all resources have been created.
For more granular control over resource lifecycles, explore the well-known lifecycle events available in Aspire’s Resource Model.
Best Practices
Section titled “Best Practices”- Keep it Simple: Start with a minimal
AppHost
and add complexity as needed. - Use Dependencies: Clearly define which services depend on others using
.WithReference(...)
. - Environment Configuration: Use different configurations for development, testing, and production.
- Resource Naming: Use clear, descriptive names for your resources to make debugging easier.