RequestGuard 1.0.4

dotnet add package RequestGuard --version 1.0.4
                    
NuGet\Install-Package RequestGuard -Version 1.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RequestGuard" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RequestGuard" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="RequestGuard" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RequestGuard --version 1.0.4
                    
#r "nuget: RequestGuard, 1.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RequestGuard@1.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RequestGuard&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=RequestGuard&version=1.0.4
                    
Install as a Cake Tool

RequestGuard

Rate Limiting, Throttling, and Concurrency Management for Your API in a Flexible Way!

RequestGuard is a powerful package that provides multiple options to handle rate limiting, concurrency, and throttling in a flexible and efficient way. This package works seamlessly with Redis, ensuring that it can be used across multiple instances without relying on in-memory solutions.

Whether you're handling high traffic or ensuring fair usage, RequestGuard gives you the tools to manage these challenges effortlessly.


🚀 Features

  • Redis-backed: Works with Redis, making it suitable for distributed environments and multiple instances.
  • Flexible Options: Apply rate limiting, throttling, and concurrency handling globally, per endpoint, or using attributes.
  • Multitenant Support: Automatically handles rate limiting and throttling per tenant based on request headers.
  • Customizable Responses: Easily configure response messages and behavior for each of the options.

🛠 How to Use

How to Use RequestGuard

To get started with the RequestGuard package in your ASP.NET Core application, follow the steps below:

Step 1: Add the Service to the Container

In your Program.cs (or Startup.cs for older versions), you need to register the services from the RequestGuard package by calling AddRequestGuards. This will bind all your configuration options from appsettings.json and register necessary services.

builder.Services.AddRequestGuards(builder.Configuration);  // Registers all the options and services from the package

Step 2: Register the Middleware You Want to Use

Next, decide which type of guard you want to apply and register it within your middleware pipeline. You can choose between Concurrency, Rate Limiting, and Throttling , or all of them.

1 - To apply concurrency control:

app.UseConcurrencyGuard();  // Adds the concurrency control middleware

2 - To apply rate limiting

app.UseRateLimitGuard(); 

3 - To apply throttling

app.UseThrottlingGuard();  

You can configure RequestGuard in your project by adding the following settings to your appsettings.json file:

appsettings.json Example

{
  "RequestGuard": {
    "RedisOptions": {
      "URL": "localhost:6379",
      "Prefix": "my-api"
    },
    "RateLimitOptions": {
      "TimeFrameMs": 1000,
      "MaxRequests": 1,
      "ResponseMessage": "Too many requests, please try again later",
      "UseJsonResponse": true,
      "Global": true,
      "Endpoints": [
        {
          "Path": "/api/someendpoint",
          "TimeFrameMs": 5000,
          "MaxRequests":5
        },
        {
          "Path": "/api/anotherendpoint",
          "TimeFrameMs": 3000,
          "MaxRequests":5
        }
      ]
    },
    "ThrottlingOptions": {
      "TimeFrameMs": 1000,
      "ResponseMessage": "Too many requests, Please Try again later",
      "UseJsonResponse": false,
      "Global": true,
      "Endpoints": [
        {
          "Path": "/api/someendpoint",
          "TimeFrameMs": 5000
        },
        {
          "Path": "/api/anotherendpoint",
          "TimeFrameMs": 3000
        }
      ]
    },
    "ConcurrencyOptions": {
      "TimeFrameMs": 1000,
      "ResponseMessage": "Concurrent request detected customized",
      "UseJsonResponse": true,
      "Global": true,
      "Endpoints": [
        {
          "Path": "/api/someendpoint",
          "TimeFrameMs": 5000
        },
        {
          "Path": "/api/anotherendpoint",
          "TimeFrameMs": 3000
        }
      ]
    },
    "MultiTenantOptions": {
      "MultiTenant": true,
      "MultiTenantHeader": "Tenant-ID"
    }
  }
}

Explanation of Options

RedisOptions: Configure the Redis connection settings.

  • URL: The Redis server address.
  • Prefix: A custom prefix to avoid naming collisions in Redis.

RateLimitOptions: Define rate limiting settings.

  • TimeFrameMs: The time frame for rate limiting (in milliseconds).
  • MaxRequests: The maximum number of requests allowed within the time frame.
  • ResponseMessage: The message returned when rate limit is exceeded.
  • UseJsonResponse: If true, returns a JSON response on rate limit exceeded.
  • Global: If true, applies the rate limit globally.
  • Endpoints: Specific rate limits for individual endpoints.

ThrottlingOptions: Define throttling settings.

  • TimeFrameMs: The time frame for throttling (in milliseconds).
  • ResponseMessage: The message returned when throttling is triggered.
  • UseJsonResponse: If true, returns a JSON response on throttling.
  • Global: If true, applies the throttling globally.
  • Endpoints: Specific throttling settings for individual endpoints.

ConcurrencyOptions: Define concurrency settings.

  • TimeFrameMs: The time frame for concurrency control (in milliseconds).
  • ResponseMessage: The message returned when concurrency is detected.
  • UseJsonResponse: If true, returns a JSON response on concurrency.
  • Global: If true, applies concurrency control globally.
  • Endpoints: Specific concurrency settings for individual endpoints.

MultiTenantOptions: Enables multitenant support.

  • MultiTenant: Set to true to enable multitenancy.
  • MultiTenantHeader: The header name that contains the tenant ID (e.g., Tenant-ID).
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 396 4/27/2025
1.0.3 379 4/27/2025
1.0.1 401 4/23/2025
1.0.0 412 4/10/2025