home / skills / aaronontheweb / dotnet-skills / aspire-configuration

aspire-configuration skill

/skills/aspire-configuration

This skill helps configure Aspire AppHost to emit explicit environment variables for app settings, keeping code free of Aspire clients and service discovery.

npx playbooks add skill aaronontheweb/dotnet-skills --skill aspire-configuration

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
5.0 KB
---
name: aspire-configuration
description: Configure Aspire AppHost to emit explicit app config via environment variables; keep app code free of Aspire clients and service discovery.
invocable: false
---

# Aspire Configuration

## When to Use This Skill

Use this skill when:
- Wiring AppHost resources to application configuration in Aspire-based repos
- Ensuring production configuration is transparent and portable outside of Aspire
- Avoiding Aspire client/service-discovery packages inside application code
- Designing feature toggles for dev/test without changing app code paths

---

## Core Principles

1. **AppHost owns Aspire infrastructure packages**
   - Aspire Hosting packages belong in AppHost only.
   - App projects should not reference Aspire client/service-discovery packages.

2. **Explicit configuration only**
   - AppHost must translate resource outputs into explicit config keys (env vars).
   - App code binds to `IOptions<T>` or `Configuration` only.

3. **Production parity and transparency**
   - Every value injected by AppHost must be representable in production as env vars
     or config files without Aspire.
   - Avoid opaque service discovery and implicit configuration.

---

## Configuration Flow

```
AppHost resource -> WithEnvironment(...) -> app config keys -> IOptions<T> in app
```

The AppHost is responsible for turning Aspire resources into explicit app settings.
The application never consumes Aspire clients or service discovery directly.

---

## AppHost Patterns (Explicit Mapping)

### Example: Database + Blob Storage

```csharp
// AppHost/Program.cs
var builder = DistributedApplication.CreateBuilder(args);

var postgres = builder.AddPostgres("postgres");
var db = postgres.AddDatabase("appdb");

var minio = builder.AddContainer("minio", "minio/minio")
    .WithArgs("server", "/data")
    .WithHttpEndpoint(targetPort: 9000, name: "http")
    .WithHttpEndpoint(targetPort: 9001, name: "console")
    .WithEnvironment("MINIO_ROOT_USER", "minioadmin")
    .WithEnvironment("MINIO_ROOT_PASSWORD", "minioadmin");

var api = builder.AddProject<Projects.MyApp_Api>("api")
    .WithReference(db, "Postgres")
    .WithEnvironment("BlobStorage__Enabled", "true")
    .WithEnvironment("BlobStorage__ServiceUrl", minio.GetEndpoint("http"))
    .WithEnvironment("BlobStorage__AccessKey", "minioadmin")
    .WithEnvironment("BlobStorage__SecretKey", "minioadmin")
    .WithEnvironment("BlobStorage__Bucket", "attachments")
    .WithEnvironment("BlobStorage__ForcePathStyle", "true");

builder.Build().Run();
```

**Key points**
- `WithReference(db, "Postgres")` sets `ConnectionStrings__Postgres` explicitly.
- Every external dependency is represented via explicit config keys.
- The API project only reads `Configuration` values.

---

## App Code Pattern (No Aspire Clients)

Application code binds to options and initializes SDKs directly. It never depends
on Aspire client packages or service discovery.

```csharp
// Api/Program.cs
builder.Services
    .AddOptions<BlobStorageOptions>()
    .BindConfiguration("BlobStorage")
    .ValidateDataAnnotations()
    .ValidateOnStart();

builder.Services.AddSingleton<IBlobStorageService>(sp =>
{
    var options = sp.GetRequiredService<IOptions<BlobStorageOptions>>().Value;
    return new S3BlobStorageService(options); // uses explicit options only
});
```

**Do not** add Aspire client packages (or `AddServiceDiscovery`) to the app.
Those are orchestration concerns and should stay in AppHost.

---

## Feature Toggles and Test Overrides

Keep toggles in config and drive them through AppHost and test fixtures. This
maintains parity between dev/test and production configuration.

```csharp
// AppHost: disable persistence in tests via config overrides
var config = builder.Configuration.GetSection("App")
    .Get<AppHostConfiguration>() ?? new AppHostConfiguration();

if (!config.UseVolumes)
{
    postgres.WithDataVolume(false);
}

api.WithEnvironment("BlobStorage__Enabled", config.EnableBlobStorage.ToString());
```

See `skills/aspire/integration-testing/SKILL.md` for patterns on passing
configuration overrides into `DistributedApplicationTestingBuilder`.

---

## Do / Don’t Checklist

**Do**
- Map every Aspire resource output to explicit configuration keys
- Use `IOptions<T>` with validation for all infrastructure settings
- Keep AppHost as the only place that references Aspire hosting packages
- Ensure any AppHost-injected value can be set in production env vars

**Don’t**
- Reference Aspire client/service-discovery packages in application projects
- Rely on opaque service discovery that cannot be mirrored in production
- Hide configuration behind Aspire-only abstractions

---

## Related Skills

- `skills/aspire/service-defaults/SKILL.md`
- `skills/aspire/integration-testing/SKILL.md`
- `skills/akka/aspire-configuration/SKILL.md`

---

## Resources

- Aspire AppHost environment configuration: https://learn.microsoft.com/en-us/dotnet/aspire/fundamentals/app-host
- Configuration in .NET: https://learn.microsoft.com/en-us/dotnet/core/extensions/configuration

Overview

This skill configures an Aspire AppHost to emit explicit application configuration via environment variables so application code remains free of Aspire clients and service discovery. It prescribes a pattern where AppHost owns Aspire hosting packages and translates resource outputs into concrete config keys. The application binds to IConfiguration/IOptions<T> and initializes SDKs from plain configuration values, preserving production parity and portability.

How this skill works

AppHost provisions Aspire resources (databases, containers, services) and maps their outputs to explicit environment variables or configuration keys using WithEnvironment and WithReference helpers. The application reads these keys through IConfiguration and IOptions<T> and instantiates SDKs (e.g., S3 client, Postgres connection) directly from those values. No Aspire client or service discovery packages are referenced by the app — orchestration concerns stay in AppHost and every injected value can be reproduced as a production env var or config file.

When to use it

  • Wiring AppHost resources to application configuration in Aspire-based repositories
  • Ensuring production configuration is transparent and portable without Aspire runtime
  • Keeping application projects free of Aspire client/service-discovery packages
  • Designing feature toggles or test overrides without changing application code paths
  • Maintaining parity between local/dev/test and production configuration

Best practices

  • Keep all Aspire hosting packages inside AppHost only; application projects must not reference them
  • Map each Aspire resource output to explicit config keys (e.g., ConnectionStrings__Postgres, BlobStorage__ServiceUrl)
  • Use IOptions<T> with data annotation validation and ValidateOnStart for infrastructure settings
  • Ensure every AppHost-injected value can be represented as an environment variable or config file in production
  • Drive feature toggles and test overrides through AppHost configuration, not by altering app code

Example use cases

  • Expose a Postgres connection string from AppHost as ConnectionStrings__Postgres and bind it to DbContext via IConfiguration
  • Provision a Minio container in AppHost and set BlobStorage__ServiceUrl/AccessKey/SecretKey for the API to consume
  • Disable persistence in test runs by toggling volumes from AppHost and passing BlobStorage__Enabled=false to the app
  • Run integration tests with DistributedApplicationTestingBuilder by overriding config keys instead of injecting Aspire clients
  • Migrate an app to Kubernetes by ensuring all AppHost-emitted env vars map directly to deployment environment variables

FAQ

Why keep Aspire references out of the application project?

Aspire hosting is an orchestration concern. Removing Aspire references keeps application code portable and ensures configuration can be replicated in production without Aspire-specific mechanisms.

How do I validate injected configuration at startup?

Bind infrastructure sections to IOptions<T>, decorate options with data annotations, and call ValidateOnStart so the app fails fast on invalid config.