/// <summary> /// Add rate limit service /// </summary> /// <param name="builder"></param> /// <param name="algorithm"></param> /// <param name="error"></param> /// <param name="interceptor"></param> /// <returns></returns> public static IServiceCollection AddRateLimit(this IServiceCollection builder, IAlgorithm algorithm, HttpErrorResponse error = null, HttpInvokeInterceptor interceptor = null) { if (algorithm == null) { throw new ArgumentNullException("The algorithm service is not registered, please use 'AddRateLimit' in 'ConfigureServices' method."); } if (error == null) { error = new HttpErrorResponse() { HttpStatusCode = 429, BuildHttpContent = (context, checkResult) => { return("too many requests"); } }; } if (interceptor == null) { interceptor = new HttpInvokeInterceptor(); } builder.AddSingleton <IAlgorithm>(algorithm); builder.AddSingleton <HttpErrorResponse>(error); builder.AddSingleton <HttpInvokeInterceptor>(interceptor); return(builder); }
/// <summary> /// Create a new instance /// </summary> /// <param name="next"></param> /// <param name="algorithm"></param> /// <param name="error"></param> /// <param name="interceptor"></param> public RateLimitMiddleware(RequestDelegate next, IAlgorithm algorithm, HttpErrorResponse error, HttpInvokeInterceptor interceptor) { _next = next; _algorithm = algorithm; _error = error; _interceptor = interceptor; }