WebRateProviderExtensions Class
Definition
- Namespace
- Bodu.Financial.ExchangeRates
- Assembly
- Bodu.Financial.ExchangeRates.DependencyInjection.dll
- Package
- Bodu.Financial.ExchangeRates.DependencyInjection 1.0.0
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
builderIFinancialServiceBuilderThe financial service builder.
httpClientNamestringThe name used to register and resolve the provider's HttpClient through IHttpClientFactory.
configurationIConfigurationAn optional configuration root or section. When supplied, the section named
sectionNameis bound intoTOptions.sectionNamestringThe configuration section name.
validateOptionsFunc<TOptions, bool>A predicate that returns true when the options are valid; used with
validationErrorMessageto wireValidateOnStart.validationErrorMessagestringThe error message reported by
ValidateOnStartwhenvalidateOptionsreturns false.getUserAgentFunc<TOptions, string>A selector that returns the
User-Agentheader value to apply to the named HttpClient, or null / white-space to omit the header.getHttpTimeoutFunc<TOptions, TimeSpan>A selector that returns the per-attempt HTTP timeout used to configure the Polly resilience handler.
configureAction<TOptions>An optional callback applied after configuration binding.
configureResilienceAction<HttpStandardResilienceOptions>An optional callback applied to the standard HTTP resilience options after the provider defaults have been set.
factoryFunc<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
TProviderThe concrete provider type, derived from WebRateProvider.
TOptionsThe options type.
Exceptions
- ArgumentNullException
Thrown when
builderis null.- ArgumentException
Thrown when
sectionNameis 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
builderIFinancialServiceBuilderThe financial service builder.
httpClientNamestringThe name used to register and resolve the provider's HttpClient through IHttpClientFactory.
configurationIConfigurationAn optional configuration root or section. When supplied, the section named
sectionNameis bound intoTOptions.sectionNamestringThe configuration section name.
validationErrorMessagestringThe error message reported by
ValidateOnStartwhen TryValidate(out string?) returns false.configureAction<TOptions>An optional callback applied after configuration binding.
configureResilienceAction<HttpStandardResilienceOptions>An optional callback applied to the standard HTTP resilience options after the provider defaults have been set.
factoryFunc<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
TProviderThe concrete provider type, derived from WebRateProvider.
TOptionsThe 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
builderis null.- ArgumentException
Thrown when
sectionNameis empty or white space.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |