Skip to content

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.

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:

AppHost.aspire
var builder = DistributedApplication.CreateBuilder(args);
// Add database service to orbit
var postgres = builder.AddPostgres("db")
.AddDatabase("appdata")
.WithDataVolume();
// Add API service and reference the database
var api = builder.AddProject<Projects.Api>("api")
.WithReference(postgres)
.WaitFor(postgres);
// Add frontend service and reference the API
var 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.

The AppHost project serves as the orchestration layer for your distributed application. When you run the AppHost:

  1. Service Discovery: Aspire automatically discovers all the services and resources defined in your AppHost
  2. Dependency Resolution: Services are started in the correct order based on their dependencies
  3. Configuration Injection: Connection strings and service endpoints are automatically injected into your services
  4. 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.

The template AppHost project contains:

  • DirectoryAspireApp.AppHost
    • DirectoryProperties
      • launchSettings.json
    • appsettings.Development.json
    • appsettings.json
    • AspireApp.AppHost.csproj
    • AppHost.cs

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.

  1. BeforeStartEvent: Raised before the AppHost starts.
  2. AfterEndpointsAllocatedEvent: Raised after the AppHost has allocated endpoints for all services.
  3. 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.

  • 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.
Edit page

Last updated:

Ask & Answer Collaborate Community Discuss Watch