Skip to content

feat: add .NET Aspire AppHost and ServiceDefaults - #1488

Merged
ardalis merged 2 commits into
mainfrom
issue-1484-add-aspire
Nov 15, 2026
Merged

ardalis merged 2 commits into
mainfrom
issue-1484-add-aspire

Conversation

@ardalis

@ardalis ardalis commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds .NET Aspire 13.6.5 to main for local development.

New projects

  • src/DevBetterWeb.AppHost (Aspire.AppHost.Sdk 13.6.5, net11.0)
    • SQL Server container (sql) with a persistent lifetime and data volume, plus the DevBetterWeb database (devbetterdb). docker-compose only defines SQL Server, so that's the only dependency modeled.
    • DevBetterWeb.Web runs with the DevBetterWeb.Web - DEV launch profile. It gets the database as ConnectionStrings:DefaultConnection, waits for the database, has an HTTP health check on /health, and is passed ApplyLocalMigrations=true.
    • ASPIRE010 is suppressed so it uses the DCP/dashboard NuGet packages, which means it runs without the Aspire CLI installed (CI included).
  • src/DevBetterWeb.ServiceDefaults: adds OpenTelemetry, health checks (/health and /alive), the standard HTTP resilience handler and service discovery.

Web wiring (Program.cs)

  • Calls AddServiceDefaults() and MapDefaultEndpoints(), replacing the ad-hoc AddHealthChecks() / MapHealthChecks("/health"). /health stays mapped in all environments, as before.
  • OpenTelemetry is only registered when OTEL_EXPORTER_OTLP_ENDPOINT is set (i.e. under Aspire). Application Insights 3.x is built on OpenTelemetry, so this keeps production telemetry unchanged.
  • Logging under Aspire: built-in logger providers are cleared and Serilog forwards to the OpenTelemetry provider (writeToProviders), so structured logs show up in the dashboard without duplicate console output. Production logging is unchanged.
  • Local migrations now run in Development when inside a container or when ApplyLocalMigrations is true (MigrationServiceExtensions.ShouldApplyLocalMigrations).
  • Pre-existing bug fix: Mediator 3 registers handlers as singletons by default. In Development, DI validation failed at startup because the singleton DomainEventHandler<T> consumed scoped IHandle<T>. In production, those handlers captured scoped services such as the DbContext. Mediator now uses ServiceLifetime.Scoped.

Resilience

  • Applies to every IHttpClientFactory client in all environments: timeouts, circuit breaker, and retries for safe methods only (DisableForUnsafeHttpMethods), so payments, webhooks and emails are never retried.
  • The Vimeo clients (VimeoApi, VimeoOEmbed, VimeoTusApi) opt out with RemoveAllResilienceHandlers(), because uploads use 60-minute timeouts.
  • Behavior change: the reCAPTCHA and Google Maps clients now get the standard timeouts (10s per attempt, 30s total). Stripe, Discord, Smtp2Go and Checkout don't use the factory, so they're unaffected.

Docs

  • README has a new Run with .NET Aspire section: prerequisites, aspire run / dotnet run --project src/DevBetterWeb.AppHost, the persistent container, where the SQL password is stored, and the telemetry/resilience notes.

Tests

  • New tests/DevBetterWeb.AppHost.Tests (Aspire.Hosting.Testing): builds the app model without starting containers, so Docker isn't needed. It asserts:
    • the SQL Server has a persistent data volume
    • the database name and parent
    • the web project waits for the database
    • the web project gets ConnectionStrings__DefaultConnection and ApplyLocalMigrations
  • Functional: /health and /alive return 200 Healthy.
  • Unit:
    • Vimeo clients have no ResilienceHandler while other clients do. Mutation-checked: removing the opt-out fails the test.
    • ShouldApplyLocalMigrations truth table.

Verification

  • dotnet BuildTestFormat.cs: the build is clean and all tests pass (AppHost 4, Functional 5, Tests 118, Unit 183).
    • The dotnet format step fails, but it fails the same way on origin/main (about 11.1k diagnostics in files this PR doesn't touch). The new files in this PR are format-clean. A repo-wide format pass should be a separate follow-up.
  • Ran the AppHost against Docker by hand:
    • sql, devbetterdb and web all reported Running/Healthy.
    • Both DbContexts migrated and seed data was created.
    • /health, /alive and / returned 200.
    • Structured logs and traces showed up in the dashboard.
  • An independent agent reviewed the change. Its findings were addressed, or recorded with reasons in .claude/plans/2026-11-15-issue-1484-add-aspire.md.

Follow-ups (not in this PR)

  • DailyCheckService and StartupNotificationService are production-only singleton hosted services that inject scoped IMediator / IRepository<T>. They should create a scope with IServiceScopeFactory. This captive dependency already existed before this PR.
  • Aspire integration tests that actually start containers (optional in the issue).

Fixes #1484

🤖 Generated with Claude Code

ardalis and others added 2 commits November 15, 2026 09:33
- Only register OpenTelemetry when an OTLP endpoint is configured so
  Application Insights telemetry in production is unchanged
- Extract and test the local-migration decision
- Test that Vimeo HttpClients opt out of the default resilience handler
- Clarify production resilience behavior in the README

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ardalis
ardalis merged commit fe990bb into main Nov 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add .NET Aspire (AppHost + ServiceDefaults) to main

1 participant