Table of Contents

WebRateProviderExtensions Class

Definition

Namespace
Bodu.Financial.ExchangeRates
Assembly
Bodu.Financial.ExchangeRates.DependencyInjection.dll
Package
Bodu.Financial.ExchangeRates.DependencyInjection 1.0.0
Source
WebRateProviderExtensions.cs

Provides the shared registration machinery for web-based exchange-rate providers on an IFinancialServiceBuilder.

public static class WebRateProviderExtensions
Inheritance
WebRateProviderExtensions
Inherited Members

Remarks

Each provider's dependency-injection package calls one of the AddWebRateProvider overloads to handle the common plumbing: binding and validating the provider's options, configuring a named HttpClient with a Polly standard-resilience handler, constructing the provider singleton, and exposing it as both IDatedRateProvider and IRateProvider. The provider-specific extension method supplies only the values that differ across providers: the HttpClient name, configuration section, validation predicate, user-agent and timeout selectors, and the factory delegate that invokes the concrete constructor.

The two public overloads differ by options-type constraint. Use the short overload when TOptions derives from WebRateProviderOptions - it reads UserAgent, HttpTimeout, and TryValidate(out string?) automatically. Use the full overload when the options type does not inherit from WebRateProviderOptions and the caller must supply explicit selectors.

Methods

AddWebRateProvider<TProvider, TOptions>(IFinancialServiceBuilder, string, IConfiguration?, string, Func<TOptions, bool>, string, Func<TOptions, string?>, Func<TOptions, TimeSpan>, Action<TOptions>?, Action<HttpStandardResilienceOptions>?, Func<HttpClient, TOptions, ILoggerFactory?, TimeProvider?, TProvider>)

Registers a web-based exchange-rate provider with an arbitrary options type, binding options and configuring a named HttpClient with Polly resilience.

public static IFinancialServiceBuilder AddWebRateProvider<TProvider, TOptions>(this IFinancialServiceBuilder builder, string httpClientName, IConfiguration? configuration, string sectionName, Func<TOptions, bool> validateOptions, string validationErrorMessage, Func<TOptions, string?> getUserAgent, Func<TOptions, TimeSpan> getHttpTimeout, Action<TOptions>? configure, Action<HttpStandardResilienceOptions>? configureResilience, Func<HttpClient, TOptions, ILoggerFactory?, TimeProvider?, TProvider> factory) where TProvider : WebRateProvider where TOptions : class

Parameters

builder IFinancialServiceBuilder

The financial service builder.

httpClientName string

The name used to register and resolve the provider's HttpClient through IHttpClientFactory.

configuration IConfiguration

An optional configuration root or section. When supplied, the section named sectionName is bound into TOptions.

sectionName string

The configuration section name.

validateOptions Func<TOptions, bool>

A predicate that returns true when the options are valid; used with validationErrorMessage to wire ValidateOnStart.

validationErrorMessage string

The error message reported by ValidateOnStart when validateOptions returns false.

getUserAgent Func<TOptions, string>

A selector that returns the User-Agent header value to apply to the named HttpClient, or null / white-space to omit the header.

getHttpTimeout Func<TOptions, TimeSpan>

A selector that returns the per-attempt HTTP timeout used to configure the Polly resilience handler.

configure Action<TOptions>

An optional callback applied after configuration binding.

configureResilience Action<HttpStandardResilienceOptions>

An optional callback applied to the standard HTTP resilience options after the provider defaults have been set.

factory Func<HttpClient, TOptions, ILoggerFactory, TimeProvider, TProvider>

A delegate that constructs the provider from an HttpClient, the resolved options, an optional ILoggerFactory, and an optional TimeProvider.

Returns

IFinancialServiceBuilder

The builder, for chaining.

Type Parameters

TProvider

The concrete provider type, derived from WebRateProvider.

TOptions

The options type.

Exceptions

ArgumentNullException

Thrown when builder is null.

ArgumentException

Thrown when sectionName is empty or white space.

AddWebRateProvider<TProvider, TOptions>(IFinancialServiceBuilder, string, IConfiguration?, string, string, Action<TOptions>?, Action<HttpStandardResilienceOptions>?, Func<HttpClient, TOptions, ILoggerFactory?, TimeProvider?, TProvider>)

Registers a web-based exchange-rate provider whose options derive from WebRateProviderOptions, binding options and configuring a named HttpClient with Polly resilience.

public static IFinancialServiceBuilder AddWebRateProvider<TProvider, TOptions>(this IFinancialServiceBuilder builder, string httpClientName, IConfiguration? configuration, string sectionName, string validationErrorMessage, Action<TOptions>? configure, Action<HttpStandardResilienceOptions>? configureResilience, Func<HttpClient, TOptions, ILoggerFactory?, TimeProvider?, TProvider> factory) where TProvider : WebRateProvider where TOptions : WebRateProviderOptions

Parameters

builder IFinancialServiceBuilder

The financial service builder.

httpClientName string

The name used to register and resolve the provider's HttpClient through IHttpClientFactory.

configuration IConfiguration

An optional configuration root or section. When supplied, the section named sectionName is bound into TOptions.

sectionName string

The configuration section name.

validationErrorMessage string

The error message reported by ValidateOnStart when TryValidate(out string?) returns false.

configure Action<TOptions>

An optional callback applied after configuration binding.

configureResilience Action<HttpStandardResilienceOptions>

An optional callback applied to the standard HTTP resilience options after the provider defaults have been set.

factory Func<HttpClient, TOptions, ILoggerFactory, TimeProvider, TProvider>

A delegate that constructs the provider from an HttpClient, the resolved options, an optional ILoggerFactory, and an optional TimeProvider.

Returns

IFinancialServiceBuilder

The builder, for chaining.

Type Parameters

TProvider

The concrete provider type, derived from WebRateProvider.

TOptions

The options type, derived from WebRateProviderOptions.

Remarks

// A provider package's own DI extension delegates to this overload.
public static IFinancialServiceBuilder AddAcmeRates(
    this IFinancialServiceBuilder builder, IConfiguration? configuration = null) =>
    builder.AddWebRateProvider<AcmeRateProvider, AcmeRateProviderOptions>(
        httpClientName: "Acme",
        configuration: configuration,
        sectionName: "Financial:Acme",
        validationErrorMessage: "Acme exchange-rate options are invalid.",
        configure: null,
        configureResilience: null,
        factory: (client, opts, loggerFactory, timeProvider) =>
            new AcmeRateProvider(client, opts, loggerFactory, timeProvider));

Exceptions

ArgumentNullException

Thrown when builder is null.

ArgumentException

Thrown when sectionName is empty or white space.

Applies to

ProductVersions
.NET8, 10