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
<PackageReference Include="RepletoryLib.Api.Middleware" Version="1.0.0" />
<PackageVersion Include="RepletoryLib.Api.Middleware" Version="1.0.0" />
<PackageReference Include="RepletoryLib.Api.Middleware" />
paket add RepletoryLib.Api.Middleware --version 1.0.0
#r "nuget: RepletoryLib.Api.Middleware, 1.0.0"
#:package RepletoryLib.Api.Middleware@1.0.0
#addin nuget:?package=RepletoryLib.Api.Middleware&version=1.0.0
#tool nuget:?package=RepletoryLib.Api.Middleware&version=1.0.0
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.
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
ErrorResponseJSON - Correlation ID -- Generates or propagates
X-Correlation-IDacross the request lifecycle - Request timing -- Adds
X-Response-Time-Msheader 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:
- CorrelationIdMiddleware -- Sets
X-Correlation-ID - RequestTimingMiddleware -- Measures elapsed time
- RequestLoggingMiddleware -- Logs request details
- TenantResolutionMiddleware -- Extracts tenant from JWT
- IpFilterMiddleware -- Applies IP filtering (if enabled)
- 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 | Versions 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. |
-
net10.0
- RepletoryLib.Common (>= 1.0.0)
- RepletoryLib.Logging (>= 1.0.0)
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 |