Exchange rates in C# and .NET
Create a typed .NET FX client with IHttpClientFactory, decimal rates, cancellation, a bounded timeout, and source-aware response handling.
Register one typed HttpClient for exchangerate.dev, inject the API key from configuration, and deserialize rate values to decimal. Keep cancellation and timeout behavior explicit so a slow upstream request cannot hold an application request open indefinitely.
IHttpClientFactory for connection reuse, DNS refresh, logging, and central client configuration.decimal and retain the API freshness metadata alongside them.CancellationToken through every request and set a bounded client timeout.EnsureSuccessStatusCode() before decoding so failed responses never look like valid rate data.Register a typed HttpClient
The registration owns the base URL, bearer token, and timeout. The typed client owns the response contract and passes cancellation from the caller to the network request.
Pass cancellation from the request
Cache successful observations only
A shared cache can absorb repeated reads, but the cache entry should contain the complete observation rather than only Rates["EUR"]. Keep Source, MarketSession, and DataUpdatedAt so the caller can distinguish a live trading-day update from a daily or closed-market value.
- Keep one typed client registration.
- Use decimal for rates and amounts.
- Pass cancellation through the full call chain.
- Reject non-success HTTP responses before deserialization.