ADR-0020: Outbound HTTP integrations — typed client, per-client resilience, validated options
- Status: accepted
- Date: 2026-09-25
- Amended: 2026-10-08 — replace (not add) the blanket pipeline, the real exception set, API-only registration, back-off-aware budget, Frankfurter (see Amendment below)
- Amended: 2026-10-11 — an outage is
Unavailable(503), a refusal staysFailure, statuses mapped by endpoint contract (see Amendment (2026-10-11) below)
Context
Section titled “Context”Every non-trivial service eventually calls something it does not own, and the template said nothing about how. Trellis.Infrastructure held exactly two adapters (a JWT minter and a logging email stub), so the first third-party integration in any generated project was a blank page — and the defaults on that page are unhelpful in ways that only show up in production:
ServiceDefaultsappliesAddStandardResilienceHandler()to everyHttpClient. Its per-attempt timeout is 10 seconds. For a fast JSON API that is fine; for anything slower it cancels every attempt regardless of whatHttpClient.Timeoutsays, and the resultingTaskCanceledExceptionlooks like a caller cancellation rather than a misconfiguration.- Leaving
HttpClient.Timeoutat its 100s default while a resilience pipeline is attached caps the whole pipeline, retries included, and races the per-attempt timeout. - A third party's exceptions are transport concerns. Letting
HttpRequestExceptionescape into Application code contradicts ADR-0004 (Result, not exceptions) at exactly the boundary where failure is most expected. - Configuration for an integration (a base URL, a timeout) is the kind of thing that is wrong in one environment only, and
Get<T>() ?? new T()turns that into a silently-defaulted runtime failure far from the cause. - A health check that calls a dependency on every probe hands anyone who can reach
/healtha free load lever against that dependency — and against whatever rate limit governs it.
Decision
Section titled “Decision”A reference outbound integration ships in src/Infrastructure/Trellis.Infrastructure/ExternalApis/ExchangeRates/, and it is the shape to copy:
- A port in Application, an adapter in Infrastructure.
IExchangeRateProviderreturnsResult<decimal>;ExchangeRateClientis aninternal sealedtypedHttpClientthat converts every transport outcome — error status, timeout, transport failure, unreadable body — into anError. Nothing a third party does reaches Application code as an exception. - Resilience is configured per client, not inherited. The client sets
HttpClient.Timeout = Timeout.InfiniteTimeSpan(the pipeline owns timeouts) and overrides the attempt timeout, total timeout, breaker sampling window and retry count from its own options.MaxRetrieshas a floor of 1 because the standard handler validatesMaxRetryAttempts >= 1. - Options are annotated and validated.
ExchangeRateOptionscarries[Required],[Url]and[Range], bound withValidateDataAnnotations().ValidateOnStart(). A malformed URL fails the host at boot, not on the first call. - The health check probes cheaply and caches.
ExchangeRateHealthCheckopens a bare TCP connection (no application request), caches the verdict for 30s, and distinguishes Degraded (third party unreachable — do not pull a healthy instance out of rotation over someone else's outage) from Unhealthy (our own configuration is wrong — scream). It must be registered as a singleton or the cache is meaningless.
The same options discipline now covers AuthTokenOptions. Binding and annotations live in AddInfrastructure; promoting them to a boot failure is a separate opt-in, RequireValidTokenOptions(), called only by the API. That split is deliberate: ValidateOnStart is host policy, and the migration service shares the Infrastructure registration but must not refuse to run EF migrations for want of a JWT secret it never uses.
Consequences
Section titled “Consequences”- The first integration in a generated project is a copy-and-adapt, not a research task — including the two traps (blanket attempt timeout,
HttpClient.Timeoutcapping the pipeline) that are invisible until load or latency finds them. Trellis.Infrastructuretakes onMicrosoft.Extensions.Http,.Http.Resilience,.Diagnostics.HealthChecksand.Options.DataAnnotations— all Microsoft, all within the ADR-0011 license ceilings./healthgains anexchange-ratesentry taggedexternal. Readiness can now report Degraded, which orchestrators treat as still-serving;/aliveis unaffected because nothing here is taggedlive.- The sample provider is part of the reference material, like the Invoices slice: delete it when the real integration replaces it.
Amendment (2026-10-08): replace the blanket pipeline, catch what it throws, register only where it is used
Section titled “Amendment (2026-10-08): replace the blanket pipeline, catch what it throws, register only where it is used”The first version taught three things that were wrong in exactly the situations it was written for.
- "Override per client" stacked instead of replacing.
AddStandardResilienceHandler(...)on a client thatServiceDefaultsalready gave one adds a secondResilienceHandler, nested inside the first: the blanket 10 s attempt timeout still cancels every attempt, and each outer retry reruns the client's whole retry loop. The client now callsRemoveAllResilienceHandlers()first (experimental,EXTEXP0001suppressed with the reason; the non-experimental equivalent removes everyResilienceHandlerinConfigureAdditionalHttpMessageHandlers) and then adds its own. Replace, don't add — a test bootsAddServiceDefaults()with the integration and asserts exactly oneResilienceHandlerin the chain. - The catch set was a bare
HttpClient's, not the pipeline's. Behind the standard handler a timed-out attempt or budget surfaces as Polly'sTimeoutRejectedException(notTaskCanceledException) and an open circuit asBrokenCircuitException; both escaped the Result boundary as 500s. The adapter now mapsHttpRequestException,TimeoutRejectedException,BrokenCircuitExceptionand any otherExecutionRejectedExceptiontoUnavailable,JsonExceptiontoMalformedResponse, and anOperationCanceledExceptionthe caller did not cause toUnavailable; the caller's own cancellation propagates. ValidateOnStartinAddInfrastructurereached the migration host — and moving it was not enough. The standard resilience handler validates its own options at boot, and the client's are computed from the integration's options, so merely registering the client makes its section a boot requirement of the host. Outbound integrations are therefore registered byAddOutboundIntegrations(configuration), which only hosts that call out invoke (the API); it validates their options at boot.AddInfrastructurekeeps what every host needs. The same call validatesEmail:Providerat boot and warns when the logging stand-in is active outside Development; the e-mail sender itself stays inAddInfrastructure, because Application's event handlers must be constructible in every host.
Two smaller corrections ride along. The pipeline reads the validated options through AddStandardResilienceHandler().Configure((o, sp) => …) when it is built, instead of a Get<T>() ?? new T() snapshot bound at registration. Its total timeout now counts the back-off — attempt × (retries + 1) + maxDelay × retries, with waits capped at Retry.MaxDelay — because a budget of attempts alone (attempt × (retries + 2)) cut off a retry the policy had granted whenever the jittered wait ran long. The default provider moved from api.exchangerate.host, which now answers success:false at HTTP 200 to every keyless call while its TCP probe stays green, to Frankfurter (https://api.frankfurter.dev/v1/, same latest?base=&symbols= shape); an explicit success:false is still rejected, and an opt-in live smoke test (RUN_LIVE_SMOKE_TESTS=1) watches the provider itself.
The default provider then moved once more, to Frankfurter's v2 API (https://api.frankfurter.dev/v2/,
rates?base="es=, an array per quote): v1 answers with a Deprecation header and a successor
link. The adapter reads the array, an object with success:false stays Unavailable, any other shape
is MalformedResponse (docs/features/exchange-rates.md).
Amendment (2026-10-11): an outage is Unavailable, a refusal stays a Failure
Section titled “Amendment (2026-10-11): an outage is Unavailable, a refusal stays a Failure”ExchangeRateErrors.Unavailable was a deliberate Failure (500), and it covered two different
situations: the provider being down, and the provider refusing us. ADR-0004 now has
ErrorType.Unavailable (503), so the reference port splits them by whether waiting helps:
ExchangeRate.Unavailableis anUnavailable: a transport failure, an attempt or total timeout, an open circuit, a concurrency-limiter rejection, and a 5xx, 408 or 429 still failing after the pipeline's retries. A client may retry later.ExchangeRate.Rejectedis aFailure: the provider answered and refused our credentials or plan (401/403), a route it does not serve, meaning a wrongBaseUrlor a retired API version (404), or saidsuccess:falsein a 200. No wait cures our configuration, so a 503 would mislead.ExchangeRate.MalformedResponsestays aFailure, andExchangeRate.UnknownCurrencyaValidation.
The status mapping follows what each status means for the endpoint called, probed against the
provider, not the status class: Frankfurter answers an unknown currency with a 422, which is the
caller's input (UnknownCurrency, a 400 at our API), while a 404 on its rates collection route can
only be our wrong URL. An upstream 401/403/404 is never forwarded as our own. The table is in
docs/features/exchange-rates.md.
An endpoint that calls an integration declares the 503 explicitly with a bare
.Produces(StatusCodes.Status503ServiceUnavailable), which the OpenAPI transformer fills with the
ProblemResponse body like any other declared status (ADR-0025): the shape of an operation cannot show
that it depends on a third party. No shipped endpoint calls the port yet, so the committed document has
no 503. Retry-After is deferred for the reason ADR-0004's amendment gives.
The documentation of Trellis.Templates 0.2.0 · release notes