Table of Contents

ThrottlingSentinelMiddleware Class

Definition

Provides an API throttling middleware implementation for ASP.NET Core.

public class ThrottlingSentinelMiddleware : ConfigurableMiddleware<IThrottlingCache, ThrottlingSentinelOptions>, IConfigurable<ThrottlingSentinelOptions>
Inheritance
ThrottlingSentinelMiddleware
Implements
Inherited Members

Examples

The following example demonstrates how to register the to enforce rate limiting in the ASP.NET Core pipeline.

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;

        namespace Cuemon.AspNetCore.Http.Throttling;

        public static class ThrottlingSentinelMiddlewareExample
        {
            public static async Task DemonstrateAsync()
            {
                var context = new DefaultHttpContext();
        var options = Options.Create(new ThrottlingSentinelOptions
        {
            ContextResolver = _ => "client-1",
            Quota = new ThrottleQuota(10, TimeSpan.FromMinutes(1))
        });

        var middleware = new ThrottlingSentinelMiddleware(
            httpContext =>
            {
                httpContext.Response.StatusCode = StatusCodes.Status200OK;
                return Task.CompletedTask;
            },
            options);

        await middleware.InvokeAsync(context, new MemoryThrottlingCache());
        Console.WriteLine(context.Response.Headers[options.Value.RateLimitHeaderName].ToString());
            }
        }

Constructors

Name Description
ThrottlingSentinelMiddleware(RequestDelegate, IOptions<ThrottlingSentinelOptions>)

Initializes a new instance of the ThrottlingSentinelMiddleware class.

ThrottlingSentinelMiddleware(RequestDelegate, Action<ThrottlingSentinelOptions>)

Initializes a new instance of the ThrottlingSentinelMiddleware class.

Methods

Name Description
InvokeAsync(HttpContext, IThrottlingCache)

Executes the ThrottlingSentinelMiddleware.