Skip to main content

RA.Utilities.Api.Middlewares

· 3 min read
Redon Alla
.NET, React, Angular Developer

Version 10.0.2​

Date Badge NuGet version

Maintenance release with response model refactoring, log template improvements, and cancellation token support.

📝 Improvements​

  • Response Model Refactoring: DefaultHeadersMiddleware now constructs BadRequestResponse directly using object-initializer syntax instead of building a BadRequestResult array and wrapping it — producing the same JSON output with cleaner code.
  • Log Template Enhancement: HttpLoggingMiddleware now includes the RequestId (X-Request-Id header value) in both request and response log templates, improving trace correlation.
  • Cancellation Token Support: Both WriteAsync and CopyToAsync calls now pass context.RequestAborted, allowing operations to be properly cancelled when the client disconnects.
  • Typo Fix: Fixed Schema → Scheme property name in the request log template.

⚠️ Deprecation Notice​

This package is deprecated. All middleware types have been consolidated into RA.Utilities.Api. See the RA.Utilities.Api middlewares documentation for the current API.

Migration: Replace RA.Utilities.Api.Middlewares with RA.Utilities.Api and update extension methods:

  • AddHttpLoggingMiddleware() → AddLoggingMiddleware()
  • app.UseMiddleware<HttpLoggingMiddleware>() → app.UseLoggingMiddleware()

Version 10.0.1​

Date Badge NuGet version

This patch release focuses on performance optimizations, improved robustness in logging, and code modernization.

✨ Improvements​

  • Performance: Optimized X-Request-Id retrieval in HttpContextExtensions by switching from LINQ to TryGetValue, ensuring efficient case-insensitive lookups.
  • Robustness: Refactored HttpLoggingMiddleware to centralize stream position management and added defensive checks for empty or non-JSON body payloads.
  • Modernization: Updated internal array initializations to use C# 12 collection expressions for better readability and performance.

Version 10.0.0​

Date Badge NuGet version

Updated the project from version 10.0.100-rc.2 to 10.0.0, marking the transition from release candidate to stable release.

Version 10.0.0-rc.2​

Date Badge NuGet version

This release focuses on significantly improving the documentation and clarifying the purpose and usage of the middlewares provided in this package. The goal is to make it easier for developers to implement robust logging and header validation in their APIs.

✨ New Features & Improvements​

  • HttpLoggingMiddleware:

    • Provides high-performance HTTP request/response logging suitable for production environments.
    • Uses Microsoft.IO.RecyclableMemoryStream to minimize memory allocations and GC pressure.
    • Integrates with RA.Utilities.Logging.Shared to produce structured logs, making them easy to query and analyze.
    • Includes configurable options to exclude specific paths (e.g., /swagger, /health) from logging.
  • DefaultHeadersMiddleware:

    • Enforces the presence of required headers, such as X-Request-Id, to ensure traceability in distributed systems.
    • Automatically returns a standardized 400 Bad Request response using models from RA.Utilities.Api.Results if a required header is missing.
    • Includes configurable options to ignore header validation for specific paths.
  • Simplified Registration:

    • Introduced extension methods (AddHttpLoggingMiddleware, AddDefaultHeadersMiddleware) for clean and simple registration in Program.cs.
  • Updated Documentation:

    • The README.md has been updated to reflect the latest usage patterns and best practices, with clear code examples for .NET 8.

🚀 Getting Started​

Register the middlewares in your Program.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpLoggingMiddleware();
builder.Services.AddDefaultHeadersMiddleware();

var app = builder.Build();

app.UseMiddleware<HttpLoggingMiddleware>();
app.UseMiddleware<DefaultHeadersMiddleware>();