RepletoryLib.Api.Middleware 1.0.0

dotnet add package RepletoryLib.Api.Middleware --version 1.0.0
                    
NuGet\Install-Package RepletoryLib.Api.Middleware -Version 1.0.0
                    
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="RepletoryLib.Api.Middleware" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RepletoryLib.Api.Middleware" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="RepletoryLib.Api.Middleware" />
                    
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 RepletoryLib.Api.Middleware --version 1.0.0
                    
#r "nuget: RepletoryLib.Api.Middleware, 1.0.0"
                    
#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 RepletoryLib.Api.Middleware@1.0.0
                    
#: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=RepletoryLib.Api.Middleware&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=RepletoryLib.Api.Middleware&version=1.0.0
                    
Install as a Cake Tool

RepletoryLib.Api.Middleware

ASP.NET Core middleware for exception handling, correlation IDs, request logging, timing, tenant resolution, and IP filtering.

Part of the RepletoryLib ecosystem -- standalone, reusable .NET 10 libraries with zero business logic.

NuGet .NET 10 License: MIT


Overview

RepletoryLib.Api.Middleware provides a comprehensive middleware suite for ASP.NET Core applications. A single call to UseRepletoryMiddleware() adds exception handling, correlation ID tracking, request/response logging, timing, tenant resolution, and optional IP filtering to your pipeline.

Key Features

  • Global exception handling -- Catches unhandled exceptions and returns standardized ErrorResponse JSON
  • Correlation ID -- Generates or propagates X-Correlation-ID across the request lifecycle
  • Request timing -- Adds X-Response-Time-Ms header to every response
  • Request logging -- Logs HTTP method, path, status code, and duration
  • Tenant resolution -- Extracts tenant context from JWT claims
  • IP filtering -- Allow/block lists for IP-based access control
  • ICurrentTenantService -- Injectable service populated by tenant resolution middleware

Installation

dotnet add package RepletoryLib.Api.Middleware

Dependencies

Package Type
RepletoryLib.Common RepletoryLib

Quick Start

using RepletoryLib.Api.Middleware;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRepletoryMiddleware();

var app = builder.Build();
app.UseRepletoryMiddleware();
{
  "Middleware": {
    "EnableRequestLogging": true,
    "EnableTiming": true,
    "EnableIpFilter": false,
    "AllowedIps": [],
    "BlockedIps": [],
    "ExcludedPaths": ["/health", "/metrics"]
  }
}

Configuration

MiddlewareOptions

Property Type Default Description
EnableRequestLogging bool true Log request method, path, status, and duration
EnableTiming bool true Add X-Response-Time-Ms response header
EnableIpFilter bool false Enable IP allow/block filtering
AllowedIps List<string> [] IP allowlist (empty = all allowed)
BlockedIps List<string> [] IP blocklist
ExcludedPaths List<string> ["/health", "/metrics"] Paths excluded from logging

Usage Examples

Middleware Pipeline Order

UseRepletoryMiddleware() registers middleware in this order:

  1. CorrelationIdMiddleware -- Sets X-Correlation-ID
  2. RequestTimingMiddleware -- Measures elapsed time
  3. RequestLoggingMiddleware -- Logs request details
  4. TenantResolutionMiddleware -- Extracts tenant from JWT
  5. IpFilterMiddleware -- Applies IP filtering (if enabled)
  6. GlobalExceptionMiddleware -- Catches and formats exceptions

Correlation ID Propagation

Incoming requests with an X-Correlation-ID header will propagate it; otherwise, a new GUID is generated:

Request: GET /api/orders
X-Correlation-ID: abc-123

Response: 200 OK
X-Correlation-ID: abc-123
X-Response-Time-Ms: 42

Global Exception Handling

Unhandled exceptions are caught and returned as standardized JSON:

{
  "statusCode": 500,
  "message": "An unexpected error occurred",
  "errors": [],
  "traceId": "00-abc123...",
  "timestamp": "2025-01-15T10:30:00Z"
}

AppException and ValidationException from RepletoryLib.Common are handled with their specific status codes.

Tenant Resolution

The middleware extracts tenant_id and tenant_name from JWT claims and populates ICurrentTenantService:

using RepletoryLib.Common.Interfaces;

public class OrderService
{
    private readonly ICurrentTenantService _tenant;

    public OrderService(ICurrentTenantService tenant) => _tenant = tenant;

    public async Task<Order> CreateOrderAsync(Order order)
    {
        order.TenantId = _tenant.TenantId;
        // ...
    }
}

IP Filtering

{
  "Middleware": {
    "EnableIpFilter": true,
    "AllowedIps": ["10.0.0.0/24", "192.168.1.100"],
    "BlockedIps": ["203.0.113.50"]
  }
}

Integration with Other RepletoryLib Packages

Package Relationship
RepletoryLib.Common AppException, ErrorResponse, ICurrentTenantService
RepletoryLib.Logging CorrelationIdEnricher reads the correlation ID set by this middleware
RepletoryLib.Tracing Correlation IDs bridge logging and distributed tracing

License

This project is licensed under the MIT License.

Copyright (c) 2024-2026 Repletory.


For complete documentation, infrastructure setup, and configuration reference, see the RepletoryLib main repository.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.0 62 3/2/2026