Class ServiceCollectionExtensions
- Namespace
- RateLimitHeaders
- Assembly
- RateLimitHeaders.dll
Extension methods for adding rate limit aware handlers to an IHttpClientBuilder.
public static class ServiceCollectionExtensions
- Inheritance
-
ServiceCollectionExtensions
- Inherited Members
Examples
Basic usage with default options:
services.AddHttpClient("MyApi")
.AddRateLimitAwareHandler();
Configuration from appsettings.json:
// appsettings.json:
// {
// "RateLimitHandler": {
// "EnableProactiveThrottling": true,
// "QuotaLowThreshold": 0.2,
// "TrackStatePerEndpoint": true
// }
// }
services.AddHttpClient("MyApi")
.AddRateLimitAwareHandler(configuration.GetSection("RateLimitHandler"));
Using IOptions<T> pattern:
services.Configure<RateLimitAwareOptions>("MyApi", configuration.GetSection("RateLimitHandler"));
services.AddHttpClient("MyApi")
.AddRateLimitAwareHandler("MyApi");
Methods
AddRateLimitAwareHandler(IHttpClientBuilder)
Adds a RateLimitAwareHandler to the HTTP client builder with default options.
public static IHttpClientBuilder AddRateLimitAwareHandler(this IHttpClientBuilder builder)
Parameters
builderIHttpClientBuilderThe HTTP client builder.
Returns
- IHttpClientBuilder
The HTTP client builder for chaining.
Examples
services.AddHttpClient("MyApi", client => client.BaseAddress = new Uri("https://api.example.com"))
.AddRateLimitAwareHandler();
Remarks
The handler is added as a transient service as required by IHttpClientFactory. Handler instances are pooled for approximately 2 minutes.
AddRateLimitAwareHandler(IHttpClientBuilder, IConfiguration)
Adds a RateLimitAwareHandler to the HTTP client builder with options bound from an IConfiguration section.
public static IHttpClientBuilder AddRateLimitAwareHandler(this IHttpClientBuilder builder, IConfiguration configuration)
Parameters
builderIHttpClientBuilderThe HTTP client builder.
configurationIConfigurationThe configuration section to bind options from.
Returns
- IHttpClientBuilder
The HTTP client builder for chaining.
Examples
// appsettings.json:
// {
// "RateLimitHandler": {
// "EnableProactiveThrottling": true,
// "QuotaLowThreshold": 0.2,
// "TrackStatePerEndpoint": true
// }
// }
services.AddHttpClient("MyApi")
.AddRateLimitAwareHandler(configuration.GetSection("RateLimitHandler"));
Remarks
The handler is added as a transient service as required by IHttpClientFactory. Handler instances are pooled for approximately 2 minutes.
Bindable properties from configuration:
EnableProactiveThrottling- boolean (default: true)QuotaLowThreshold- double between 0.0 and 1.0 (default: 0.1)TrackStatePerEndpoint- boolean (default: true)
AddRateLimitAwareHandler(IHttpClientBuilder, IConfiguration, Action<RateLimitAwareOptions>)
Adds a RateLimitAwareHandler to the HTTP client builder with options bound from an IConfiguration section, with an additional configure action for callbacks.
public static IHttpClientBuilder AddRateLimitAwareHandler(this IHttpClientBuilder builder, IConfiguration configuration, Action<RateLimitAwareOptions> configure)
Parameters
builderIHttpClientBuilderThe HTTP client builder.
configurationIConfigurationThe configuration section to bind options from.
configureAction<RateLimitAwareOptions>An action to configure additional options (typically callbacks).
Returns
- IHttpClientBuilder
The HTTP client builder for chaining.
Examples
services.AddHttpClient("MyApi")
.AddRateLimitAwareHandler(
configuration.GetSection("RateLimitHandler"),
options => options.OnQuotaLow = args => logger.LogWarning("Quota low!"));
Remarks
This overload allows combining configuration file settings with programmatic callback setup. The configuration is applied first, then the configure action is invoked.
AddRateLimitAwareHandler(IHttpClientBuilder, Action<RateLimitAwareOptions>?)
Adds a RateLimitAwareHandler to the HTTP client builder with the specified options.
public static IHttpClientBuilder AddRateLimitAwareHandler(this IHttpClientBuilder builder, Action<RateLimitAwareOptions>? configure)
Parameters
builderIHttpClientBuilderThe HTTP client builder.
configureAction<RateLimitAwareOptions>An optional action to configure the handler options.
Returns
- IHttpClientBuilder
The HTTP client builder for chaining.
Examples
services.AddHttpClient("MyApi")
.AddRateLimitAwareHandler(options =>
{
options.EnableProactiveThrottling = true;
options.QuotaLowThreshold = 0.2;
options.OnQuotaLow = args => Console.WriteLine($"Quota low: {args.RateLimitInfo.Remaining}");
});
Remarks
The handler is added as a transient service as required by IHttpClientFactory. Handler instances are pooled for approximately 2 minutes.
AddRateLimitAwareHandler(IHttpClientBuilder, Action<IServiceProvider, RateLimitAwareOptions>)
Adds a RateLimitAwareHandler to the HTTP client builder with options configured from a callback that has access to the service provider.
public static IHttpClientBuilder AddRateLimitAwareHandler(this IHttpClientBuilder builder, Action<IServiceProvider, RateLimitAwareOptions> configure)
Parameters
builderIHttpClientBuilderThe HTTP client builder.
configureAction<IServiceProvider, RateLimitAwareOptions>An action to configure the handler options with access to the service provider.
Returns
- IHttpClientBuilder
The HTTP client builder for chaining.
Examples
services.AddHttpClient("MyApi")
.AddRateLimitAwareHandler((sp, options) =>
{
var telemetry = sp.GetRequiredService<ITelemetryService>();
options.OnRateLimitInfo = args => telemetry.TrackRateLimit(args.RateLimitInfo);
});
Remarks
The handler is added as a transient service as required by IHttpClientFactory. Handler instances are pooled for approximately 2 minutes.
AddRateLimitAwareHandler(IHttpClientBuilder, string)
Adds a RateLimitAwareHandler to the HTTP client builder using named options from the IOptionsMonitor<TOptions> pattern.
public static IHttpClientBuilder AddRateLimitAwareHandler(this IHttpClientBuilder builder, string optionsName)
Parameters
builderIHttpClientBuilderThe HTTP client builder.
optionsNamestringThe name of the options to use. Use DefaultName for unnamed options.
Returns
- IHttpClientBuilder
The HTTP client builder for chaining.
Examples
// Register options with a name matching the client name
services.Configure<RateLimitAwareOptions>("MyApi", options =>
{
options.EnableProactiveThrottling = true;
options.QuotaLowThreshold = 0.2;
});
// Or bind from configuration
services.Configure<RateLimitAwareOptions>("MyApi", configuration.GetSection("RateLimitHandler"));
// Use the named options
services.AddHttpClient("MyApi")
.AddRateLimitAwareHandler("MyApi");
Remarks
This overload integrates with the IOptions<T> pattern, allowing options to be registered separately and resolved at runtime. This is useful for scenarios where options need to be shared across multiple components or when using options validation.