home / skills / stuartf303 / sorcha / aspire

aspire skill

/.claude/skills/aspire

This skill helps configure .NET Aspire AppHost with service discovery, telemetry, health checks, and defaults for seamless multi-service orchestration.

npx playbooks add skill stuartf303/sorcha --skill aspire

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

Files (3)
SKILL.md
4.4 KB
---
name: aspire
description: |
  Configures .NET Aspire orchestration, service discovery, and telemetry.
  Use when: Adding services to AppHost, configuring service defaults, setting up health checks, or troubleshooting service discovery.
allowed-tools: Read, Edit, Write, Glob, Grep, Bash, mcp__context7__resolve-library-id, mcp__context7__query-docs
---

# .NET Aspire Skill

.NET Aspire provides orchestration for this distributed ledger platform. The AppHost (`src/Apps/Sorcha.AppHost/AppHost.cs`) orchestrates 7 microservices with PostgreSQL, MongoDB, and Redis. Services use `AddServiceDefaults()` for consistent OpenTelemetry, health checks, and service discovery. JWT signing keys are generated once and shared across all services via environment variables.

## Quick Start

### Adding a Service to AppHost

```csharp
// In AppHost.cs - Add project with resource references
var myService = builder.AddProject<Projects.Sorcha_MyService>("my-service")
    .WithReference(redis)                                    // Service discovery
    .WithReference(walletDb)                                // Database connection
    .WithEnvironment("JwtSettings__SigningKey", jwtSigningKey); // Shared config
```

### Consuming Aspire in a Service

```csharp
// In Program.cs - Every service starts with this
var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();                    // OpenTelemetry, health checks, discovery
builder.AddRedisOutputCache("redis");            // Reference by resource name
builder.AddRedisDistributedCache("redis");

var app = builder.Build();
app.MapDefaultEndpoints();                       // /health and /alive
```

## Key Concepts

| Concept | Usage | Example |
|---------|-------|---------|
| Resource Name | Identifier for service discovery | `"redis"`, `"tenant-service"` |
| WithReference | Injects connection string/URL | `.WithReference(postgres)` |
| WithEnvironment | Pass config to service | `.WithEnvironment("Key", value)` |
| WithExternalHttpEndpoints | Expose outside Aspire network | `.WithExternalHttpEndpoints()` |
| AddServiceDefaults | Shared Aspire configuration | `builder.AddServiceDefaults()` |

## Common Patterns

### Database Resources

```csharp
// PostgreSQL with multiple databases
var postgres = builder.AddPostgres("postgres").WithPgAdmin();
var tenantDb = postgres.AddDatabase("tenant-db", "sorcha_tenant");
var walletDb = postgres.AddDatabase("wallet-db", "sorcha_wallet");

// MongoDB for document storage
var mongodb = builder.AddMongoDB("mongodb").WithMongoExpress();
var registerDb = mongodb.AddDatabase("register-db", "sorcha_register");

// Redis for caching
var redis = builder.AddRedis("redis").WithRedisCommander();
```

### Service Dependencies

```csharp
// Service references other services for discovery
var validatorService = builder.AddProject<Projects.Sorcha_Validator_Service>("validator-service")
    .WithReference(redis)
    .WithReference(walletService)
    .WithReference(registerService)
    .WithReference(peerService)
    .WithReference(blueprintService);
```

### Health Endpoints

```csharp
// ServiceDefaults provides these automatically
app.MapHealthChecks("/health");           // Readiness - all checks
app.MapHealthChecks("/alive", new HealthCheckOptions
{
    Predicate = r => r.Tags.Contains("live")  // Liveness - tagged checks only
});
```

## See Also

- [patterns](references/patterns.md) - AppHost patterns, resource configuration
- [workflows](references/workflows.md) - Adding services, debugging, deployment

## Related Skills

- See the **dotnet** skill for .NET 10 patterns
- See the **minimal-apis** skill for endpoint configuration
- See the **redis** skill for cache configuration
- See the **postgresql** skill for database patterns
- See the **mongodb** skill for document storage
- See the **docker** skill for containerization

## Documentation Resources

> Fetch latest .NET Aspire documentation with Context7.

**How to use Context7:**
1. Use `mcp__context7__resolve-library-id` to search for "aspire"
2. **Prefer website documentation** (IDs starting with `/websites/`) over source code repositories when available
3. Query with `mcp__context7__query-docs` using the resolved library ID

**Library ID:** `/dotnet/docs-aspire` _(High reputation, 3264 code snippets)_

**Recommended Queries:**
- "AppHost service orchestration configuration"
- "service discovery WithReference patterns"
- "OpenTelemetry observability setup"
- "health checks readiness liveness"

Overview

This skill configures .NET Aspire orchestration, service discovery, and telemetry for microservices running under the AppHost. It shows how to register database and cache resources, add services to the host, and apply consistent defaults like OpenTelemetry, health checks, and shared configuration values such as JWT signing keys.

How this skill works

AppHost centralises service registration by declaring resource objects (Postgres, MongoDB, Redis) and adding projects with WithReference and WithEnvironment to wire discovery and shared configuration. Each service bootstraps with builder.AddServiceDefaults(), which registers OpenTelemetry, health endpoints, and discovery integrations so services can locate resources by resource name. Health and liveness endpoints are mapped automatically and resource references inject connection strings or URLs into service environments.

When to use it

  • When adding a new microservice to the AppHost and wiring its dependencies.
  • When standardising OpenTelemetry, health checks, and discovery across services.
  • When configuring databases, caches, or external endpoints for multiple services.
  • When sharing secrets or common settings (e.g., JWT signing key) across services.
  • When troubleshooting service discovery or health checks in a local or orchestrated environment.

Best practices

  • Declare shared resources once in AppHost and reference them with semantic resource names.
  • Use WithEnvironment to inject secrets and non-sensitive config via environment variables consistently.
  • Start every service with builder.AddServiceDefaults() to ensure uniform telemetry and health behavior.
  • Tag health checks for liveness vs readiness and expose both /alive and /health endpoints.
  • Prefer resource references (.WithReference) over hard-coded connection strings to keep services portable.

Example use cases

  • Add a new API service: register it in AppHost, WithReference(postgres) and WithReference(redis), then call AddServiceDefaults in the service Program.cs.
  • Create multi-database Postgres resources: define a postgres resource and call AddDatabase for tenant and wallet DBs to produce distinct connection strings.
  • Enable shared JWT signing: generate the signing key once in AppHost and pass it to services with WithEnvironment("JwtSettings__SigningKey", key).
  • Expose an internal service externally: use WithExternalHttpEndpoints() for a service that must be reachable outside the Aspire network.
  • Troubleshoot discovery issues: verify resource names used in WithReference match the declared resource identifiers and check /health for failing checks.

FAQ

Do I have to call AddServiceDefaults in every service?

Yes. AddServiceDefaults registers observability, discovery, and health checks consistently; skipping it will require you to configure those concerns manually.

How are connection strings shared with services?

AppHost injects connection strings and endpoint URLs via WithReference and WithEnvironment, exposing values as environment variables inside each service.