Class ServiceCollectionExtensions
- Namespace
- Cuemon.Extensions.AspNetCore.Http.Throttling
- Assembly
- Cuemon.Extensions.AspNetCore.dll
Extension methods for the IServiceCollection interface.
public static class ServiceCollectionExtensions
- Inheritance
-
ServiceCollectionExtensions
Examples
ServiceCollectionExtensions in the Throttling namespace registers rate-limiting services in an ASP.NET Core IServiceCollection. This example calls AddMemoryThrottlingCache and AddThrottlingCache<MemoryThrottlingCache> to register in-memory and custom throttling cache implementations as singletons, then configures ThrottlingSentinelOptions with a ContextResolver that uses the remote IP address, a quota of 100 requests per minute, custom rate-limit header names (X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset), and RetryAfter header behavior in delta-seconds format. After this setup in ConfigureServices, the middleware pipeline can enforce the configured throttling rules for each client.
using System;
using Cuemon;
using Cuemon.AspNetCore.Http.Headers;
using Cuemon.AspNetCore.Http.Throttling;
using Cuemon.Extensions.AspNetCore.Http.Throttling;
using Microsoft.Extensions.DependencyInjection;
namespace MyApp.AspNetCore.Throttling
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Register the in-memory throttling cache as a singleton
services.AddMemoryThrottlingCache();
// Register a custom IThrottlingCache implementation
services.AddThrottlingCache<MemoryThrottlingCache>();
// Configure throttling sentinel options (rate limiting rules)
services.AddThrottlingSentinelOptions(o =>
{
// Identify clients by their remote IP address
o.ContextResolver = ctx => ctx.Connection.RemoteIpAddress?.ToString() ?? "unknown";
// Allow 100 requests per minute per client
o.Quota = new ThrottleQuota(100, 1, TimeUnit.Minutes);
// Customize the rate limit response message
o.TooManyRequestsMessage = "API rate limit exceeded. Please wait before retrying.";
// Customize HTTP header names
o.RateLimitHeaderName = "X-RateLimit-Limit";
o.RateLimitRemainingHeaderName = "X-RateLimit-Remaining";
o.RateLimitResetHeaderName = "X-RateLimit-Reset";
// Use delta-seconds format for reset headers
o.RateLimitResetScope = RetryConditionScope.DeltaSeconds;
o.UseRetryAfterHeader = true;
});
}}
}
Methods
AddMemoryThrottlingCache(IServiceCollection)
Adds a MemoryThrottlingCache service to the specified IServiceCollection.
public static IServiceCollection AddMemoryThrottlingCache(this IServiceCollection services)
Parameters
servicesIServiceCollectionThe IServiceCollection to add services to.
Returns
- IServiceCollection
An IServiceCollection that can be used to further configure other services.
AddThrottlingCache<T>(IServiceCollection)
Adds a throttling cache service to the specified IServiceCollection.
public static IServiceCollection AddThrottlingCache<T>(this IServiceCollection services) where T : class, IThrottlingCache
Parameters
servicesIServiceCollectionThe IServiceCollection to add services to.
Returns
- IServiceCollection
An IServiceCollection that can be used to further configure other services.
Type Parameters
T
AddThrottlingSentinelOptions(IServiceCollection, Action<ThrottlingSentinelOptions>)
Adds configuration of ThrottlingSentinelOptions for the application.
public static IServiceCollection AddThrottlingSentinelOptions(this IServiceCollection services, Action<ThrottlingSentinelOptions> setup = null)
Parameters
servicesIServiceCollectionThe IServiceCollection to extend.
setupAction<ThrottlingSentinelOptions>The ThrottlingSentinelOptions which may be configured.
Returns
- IServiceCollection
A reference to
servicesso that additional configuration calls can be chained.
Exceptions
- ArgumentNullException
servicescannot be null.- ArgumentException
setupfailed to configure an instance of ThrottlingSentinelOptions in a valid state.