Beispiel #1
0
        /// <summary>
        /// Add rate limit service
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="algorithm"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static IServiceCollection AddRateLimit(this IServiceCollection builder, IAlgorithm algorithm, HttpRateLimitError error = null)
        {
            if (algorithm == null)
            {
                throw new ArgumentNullException("The algorithm service is not registered, please use 'AddRateLimit' in 'ConfigureServices' method.");
            }

            if (error == null)
            {
                error = new HttpRateLimitError()
                {
                    HttpStatusCode   = 429,
                    BuildHttpContent = (context, checkResult) =>
                    {
                        return("too many requests");
                    }
                };
            }

            builder.AddSingleton <IAlgorithm>(algorithm);
            builder.AddSingleton <HttpRateLimitError>(error);
            return(builder);
        }
Beispiel #2
0
 /// <summary>
 /// Create a new instance
 /// </summary>
 /// <param name="next"></param>
 /// <param name="algorithm"></param>
 /// <param name="error"></param>
 public RateLimitMiddleware(RequestDelegate next, IAlgorithm algorithm, HttpRateLimitError error)
 {
     _next      = next;
     _algorithm = algorithm;
     _error     = error;
 }