Table of Contents

WebRateProviderOptions Class

Definition

Namespace
Bodu.Financial.ExchangeRates
Assembly
Bodu.Financial.ExchangeRates.dll
Package
Bodu.Financial.ExchangeRates 1.0.0
Source
WebRateProviderOptions.cs

Provides the configuration common to every pair-based web exchange-rate source: the endpoint base address, the HTTP contract (user agent and timeout), the synchronous-access and look-back behaviour, the currency-alias map, and the per-concern diagnostic log levels. A concrete source derives from this type to add its own endpoint members and validation.

public abstract class WebRateProviderOptions
Inheritance
WebRateProviderOptions
Derived
Inherited Members
Extension Methods

Remarks

Every shared member carries a working default so the options bind cleanly through Microsoft.Extensions.Options and require no configuration for the common case. A derived type sets BaseAddress in its constructor - the base leaves it unset because the host differs per source - and overrides TryValidateCore(out string?) to add its own invariants.

The *LogLevel members set the LogLevel at which each diagnostic the provider emits is logged, so consumers can re-tune verbosity per concern without category-wide log filters. Set any of them to None to suppress that event entirely.

using Bodu.Financial.ExchangeRates;

// Concrete options types (YahooRateProviderOptions, OfxRateProviderOptions, ...) share this surface.
var options = new YahooRateProviderOptions
{
    HttpTimeout = TimeSpan.FromSeconds(10),
    AllowSynchronousNetworkAccess = false,     // force callers onto the asynchronous surface
    DefaultLookback = TimeSpan.FromDays(14),   // window used by the timeless lookup surface
};

options.CurrencyAliases["CNH"] = "CNY";        // map a non-ISO source symbol onto its ISO code

if (!options.TryValidate(out string? error))
    throw new InvalidOperationException(error);

Constructors

WebRateProviderOptions()

protected WebRateProviderOptions()

Properties

AllowSynchronousNetworkAccess

Gets or sets a value indicating whether a synchronous lookup may block to fetch a missing pair on demand.

public bool AllowSynchronousNetworkAccess { get; set; }

Property Value

bool

true to allow synchronous, blocking fetches from IDatedRateProvider lookups; false to serve only already-loaded data. Defaults to false.

Remarks

Blocking on network I/O from a synchronous method can deadlock in environments with a single-threaded synchronization context (classic ASP.NET, WPF, WinForms), so the default is snapshot-only. Leave this false and warm the store at startup; set it to true only to opt in to a blocking on-demand fetch from the synchronous lookup path.

BaseAddress

Gets or sets the base address of the source's API host. Should end with a trailing slash so relative paths resolve as expected.

public Uri BaseAddress { get; set; }

Property Value

Uri

The base address; set by the derived source's constructor.

CurrencyAliases

Gets or sets the map from ISO 4217 codes to the symbol component the source uses, applied while building a request. Codes absent from the map pass through unchanged.

public IDictionary<string, string> CurrencyAliases { get; set; }

Property Value

IDictionary<string, string>

The alias map; defaults to an empty map.

DefaultLookback

Gets or sets the look-back window used when a synchronous or undated lookup must fetch a pair on demand. The provider fetches the window ending on the requested date and spanning this duration.

public TimeSpan DefaultLookback { get; set; }

Property Value

TimeSpan

The look-back window; defaults to 7 days.

DownloadCompletedLogLevel

Gets or sets the level at which a completed pair download (with its observation count) is logged.

public LogLevel DownloadCompletedLogLevel { get; set; }

Property Value

LogLevel

The log level; defaults to Information.

DownloadFailedLogLevel

Gets or sets the level at which a failed pair download is logged.

public LogLevel DownloadFailedLogLevel { get; set; }

Property Value

LogLevel

The log level; defaults to Warning.

DownloadStartingLogLevel

Gets or sets the level at which the start of a pair download is logged.

public LogLevel DownloadStartingLogLevel { get; set; }

Property Value

LogLevel

The log level; defaults to Debug.

HistoryAvailability

Gets or sets the history depth the provider advertises: how far back it can serve rates. A caller can consult it before requesting an old date.

public RateHistoryAvailability HistoryAvailability { get; set; }

Property Value

RateHistoryAvailability

The advertised availability; defaults to Unbounded. A source whose feed publishes only the recent past sets a rolling window in its constructor.

HttpTimeout

Gets or sets the HTTP request timeout applied to requests by the dependency-injection registration.

public TimeSpan HttpTimeout { get; set; }

Property Value

TimeSpan

The HTTP timeout; defaults to 30 seconds.

MaxResponseContentBufferSize

Gets or sets the maximum number of response bytes a provider-owned HttpClient buffers, bounding the memory a single response can consume.

public long MaxResponseContentBufferSize { get; set; }

Property Value

long

The response buffer cap, in bytes; defaults to DefaultMaxResponseContentBufferSize (64 MiB).

Remarks

This value is applied when the provider creates and owns its own HttpClient. When a client is supplied to the provider directly, bounding its response size is the caller's responsibility and this value is not applied.

ObservationIngestedLogLevel

Gets or sets the level at which each individual ingested rate observation is logged.

public LogLevel ObservationIngestedLogLevel { get; set; }

Property Value

LogLevel

The log level; defaults to Information.

SynchronousNetworkFetchLogLevel

Gets or sets the level at which a synchronous, blocking on-demand network fetch is logged.

public LogLevel SynchronousNetworkFetchLogLevel { get; set; }

Property Value

LogLevel

The log level; defaults to Warning.

UserAgent

Gets or sets the User-Agent header applied to the HTTP client.

public string UserAgent { get; set; }

Property Value

string

The user-agent string; defaults to a browser-like identifier, because the public exchange-rate endpoints these options address commonly reject requests that do not present a recognizable user agent.

Remarks

This value is applied when the provider creates and owns its own HttpClient and by the dependency-injection registration when it configures the named client. When an HttpClient is supplied to the provider directly, configuring its headers is the caller's responsibility and this value is not applied.

Methods

MapCurrency(string)

Maps an ISO code through CurrencyAliases, returning the code unchanged when no alias exists.

protected string MapCurrency(string isoCode)

Parameters

isoCode string

The ISO code to map.

Returns

string

The aliased symbol component, or isoCode when unmapped.

TryValidate(out string?)

Attempts to validate the options without throwing, reporting the first invariant that is violated.

public bool TryValidate(out string? error)

Parameters

error string

When this method returns false, a message describing the first violated invariant; otherwise null.

Returns

bool

true when every invariant holds; otherwise false.

Remarks

The shared invariants are checked first, then TryValidateCore(out string?) is consulted for source-specific invariants. The throwing Validate() method is expressed in terms of this method, and the dependency-injection registration wires it into ValidateOnStart so misconfiguration fails fast at application startup.

TryValidateCore(out string?)

Validates the source-specific invariants. The default implementation reports success; a derived type overrides it to validate its own members and is invoked only after the shared invariants hold.

protected virtual bool TryValidateCore(out string? error)

Parameters

error string

When this method returns false, a message describing the violated invariant; otherwise null.

Returns

bool

true when every source-specific invariant holds; otherwise false.

Validate()

Validates the options, throwing when a required value is missing or an invariant is violated.

public void Validate()

Exceptions

ArgumentException

Thrown when an invariant is violated.

Applies to

ProductVersions
.NET8, 10