BoeRateProvider Class
Definition
- Namespace
- Bodu.Financial.ExchangeRates
- Assembly
- Bodu.Financial.ExchangeRates.Boe.dll
- Package
- Bodu.Financial.ExchangeRates.Boe 0.7.1
- Source
- BoeRateProvider.cs
Serves Bank of England daily spot rates as ExchangeRate values, implementing the Bodu.Financial provider contracts over data queried from the Bank's Interactive Statistical Database (IADB) CSV endpoint.
public sealed class BoeRateProvider : WebRateProvider, IDatedRateProvider, IRateProvider, IPairRateLoader, IHistoricalRateProvider, IDisposable
- Inheritance
-
BoeRateProvider
- Implements
- Inherited Members
- Extension Methods
Examples
using var boe = new BoeRateProvider(new BoeRateProviderOptions());
await boe.LoadRangeAsync(new DateOnly(2023, 1, 1), new DateOnly(2023, 12, 31));
RateLookupResult gbp = boe.GetRate("GBP", "USD", new DateOnly(2023, 1, 3));
// The reverse direction (USD->GBP) is served by inverting the GBP-based series.
Remarks
The provider derives from WebRateProvider, which supplies the in-memory accumulator, the immutable snapshot, the full synchronous and asynchronous lookup matrix, and ownership of the HttpClient when this provider creates one. The IADB is queried by date range, so loading is range-based: each load fetches the requested inclusive range and accumulates it. Use LoadRangeAsync(DateOnly, DateOnly, CancellationToken) to warm a range.
HttpClient ownership. The constructor that takes only options builds and owns an HttpClient configured from UserAgent and HttpTimeout, disposing it with the provider. The constructor that takes an HttpClient uses the caller-supplied client as-is; this is the path the dependency-injection package uses.
Logging. When an ILogger is supplied (directly or through the dependency-injection
package) the provider records: the start of a range download (Debug), a completed download
with its observation count (Information), each ingested observation (
Information), and a failed download (Warning, then re-thrown). Every
level is configurable through the corresponding *LogLevel property on BoeRateProviderOptions;
omitting the logger selects Instance, so logging is opt-in and free when unused.
Constructors
BoeRateProvider(BoeRateProviderOptions, ILogger?, TimeProvider?)
Initializes a new instance of the BoeRateProvider class backed by an HttpClient the provider creates and owns, configured from the supplied options.
public BoeRateProvider(BoeRateProviderOptions options, ILogger? logger = null, TimeProvider? timeProvider = null)
Parameters
optionsBoeRateProviderOptionsThe provider options.
loggerILoggertimeProviderTimeProvider
Exceptions
- ArgumentNullException
Thrown when
optionsis null.- ArgumentException
Thrown when
optionsfails validation.
BoeRateProvider(HttpClient, BoeRateProviderOptions, ILogger?, TimeProvider?)
Initializes a new instance of the BoeRateProvider class backed by the IADB CSV endpoint, queried with the caller-supplied HTTP client. The caller owns the client's configuration and lifetime.
public BoeRateProvider(HttpClient httpClient, BoeRateProviderOptions options, ILogger? logger = null, TimeProvider? timeProvider = null)
Parameters
httpClientHttpClientThe HTTP client used to download range responses.
optionsBoeRateProviderOptionsThe provider options.
loggerILoggertimeProviderTimeProvider
Exceptions
- ArgumentNullException
Thrown when
httpClientoroptionsis null.- ArgumentException
Thrown when
optionsfails validation.
Fields
BaseCurrency
The base currency the Bank of England quotes against.
public const CurrencyCode BaseCurrency = GBP
Field Value
ProviderName
The provider identifier stamped on every rate this provider produces.
public const string ProviderName = "BoE"
Field Value
Properties
AllowSynchronousNetworkAccess
Gets a value indicating whether a synchronous lookup may block to fetch a missing window on demand.
protected override bool AllowSynchronousNetworkAccess { get; }
Property Value
Remarks
When enabled, the synchronous getters block on the async fetch, which can deadlock if invoked on a thread carrying a captured SynchronizationContext (classic ASP.NET, a WPF/WinForms UI thread). The synchronous path guards against this by throwing InvalidOperationException when Current is non-null; enable this only for code that calls the getters from a thread-pool thread (or use the asynchronous API).
DefaultLookback
Gets the look-back window used when a single-rate lookup must fetch on demand; the provider fetches the window ending on the requested date and spanning this duration.
protected override TimeSpan DefaultLookback { get; }
Property Value
- TimeSpan
The look-back window.
HistoryAvailability
Gets the history depth this provider advertises: how far back it can serve rates.
public override RateHistoryAvailability HistoryAvailability { get; }
Property Value
- RateHistoryAvailability
The advertised availability; the base reports Unbounded. A derived type whose feed publishes only a bounded window overrides this to declare it.
Remarks
Forwards HistoryAvailability, which defaults to a fixed floor of 2 January 1975 - the inception of the Bank of England's daily spot exchange-rate series.
ProviderId
Gets the provider identifier stamped on every rate this provider produces.
protected override string ProviderId { get; }
Property Value
- string
The provider identifier.
Methods
EnsureLoadedAsync(CurrencyPair, DateOnly, DateOnly, CancellationToken)
Ensures the inclusive window for a pair has been fetched and accumulated, idempotently. Implementations perform their own coverage check, request coalescing, fetch, and accumulation (via AddObservations(IEnumerable<ExchangeRate>, DateTimeOffset?) and RebuildSnapshot() under SyncRoot).
protected override ValueTask EnsureLoadedAsync(CurrencyPair pair, DateOnly startDate, DateOnly endDate, CancellationToken cancellationToken)
Parameters
pairCurrencyPairThe currency pair to ensure data for. Feeds that fetch by range, feed, or file may ignore it.
startDateDateOnlyThe inclusive start of the window.
endDateDateOnlyThe inclusive end of the window.
cancellationTokenCancellationTokenA token to observe while awaiting the fetch.
Returns
- ValueTask
A task that completes when the window has been loaded.
FormatRateNotFound(string, string, DateOnly)
Formats the message for the KeyNotFoundException thrown when a single-rate lookup fails. Derived types may override to use a feed-specific resource string.
protected override string FormatRateNotFound(string fromIsoCode, string toIsoCode, DateOnly date)
Parameters
fromIsoCodestringThe source-currency ISO code.
toIsoCodestringThe destination-currency ISO code.
dateDateOnlyThe requested date.
Returns
- string
The exception message.
GetAvailablePairs()
Gets the currency pairs discovered across the ranges loaded so far.
public IReadOnlyCollection<BoeSeriesInfo> GetAvailablePairs()
Returns
- IReadOnlyCollection<BoeSeriesInfo>
A snapshot of the discovered series, one per currency pair.
IsLoaded(CurrencyPair, DateOnly, DateOnly)
Reports whether the inclusive window for a pair has already been fetched, so the synchronous lookup path can skip a redundant blocking fetch.
protected override bool IsLoaded(CurrencyPair pair, DateOnly startDate, DateOnly endDate)
Parameters
pairCurrencyPairThe currency pair to test.
startDateDateOnlyThe inclusive start of the window.
endDateDateOnlyThe inclusive end of the window.
Returns
LoadRangeAsync(DateOnly, DateOnly, CancellationToken)
Downloads and loads the inclusive date range, unless it is already covered by a previous load.
public Task LoadRangeAsync(DateOnly startDate, DateOnly endDate, CancellationToken cancellationToken = default)
Parameters
startDateDateOnlyThe inclusive start of the range.
endDateDateOnlyThe inclusive end of the range.
cancellationTokenCancellationTokenA token to observe while awaiting the load.
Returns
- Task
A task that completes when the range has been loaded.
Exceptions
- ArgumentException
Thrown when
endDateprecedesstartDate.
OnObservationIngested(ExchangeRate)
Called once per observation as it is ingested, for derived-type diagnostics. The default does nothing.
protected override void OnObservationIngested(ExchangeRate rate)
Parameters
rateExchangeRateThe observation being ingested.
ValidateRangeRequest(string, string, DateOnly, DateOnly)
Validates a range request against feed-specific preconditions before any fetch is attempted. The default does nothing; derived types may override to reject unsupported pairs (for example, a single-issuer feed that quotes only against one base currency).
protected override void ValidateRangeRequest(string fromIsoCode, string toIsoCode, DateOnly startDate, DateOnly endDate)
Parameters
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |