HoneyDrunk.Web.Rest.AspNetCore
0.2.0
dotnet add package HoneyDrunk.Web.Rest.AspNetCore --version 0.2.0
NuGet\Install-Package HoneyDrunk.Web.Rest.AspNetCore -Version 0.2.0
<PackageReference Include="HoneyDrunk.Web.Rest.AspNetCore" Version="0.2.0" />
<PackageVersion Include="HoneyDrunk.Web.Rest.AspNetCore" Version="0.2.0" />
<PackageReference Include="HoneyDrunk.Web.Rest.AspNetCore" />
paket add HoneyDrunk.Web.Rest.AspNetCore --version 0.2.0
#r "nuget: HoneyDrunk.Web.Rest.AspNetCore, 0.2.0"
#:package HoneyDrunk.Web.Rest.AspNetCore@0.2.0
#addin nuget:?package=HoneyDrunk.Web.Rest.AspNetCore&version=0.2.0
#tool nuget:?package=HoneyDrunk.Web.Rest.AspNetCore&version=0.2.0
HoneyDrunk.Web.Rest.AspNetCore
Middleware, filters, and endpoint helpers that enforce HoneyDrunk REST conventions for ASP.NET Core services.
Quick Start
var builder = WebApplication.CreateBuilder(args);
// Add REST services
builder.Services.AddRest(options =>
{
options.IncludeExceptionDetails = builder.Environment.IsDevelopment();
options.EnableRequestLoggingScope = true;
});
var app = builder.Build();
// Use REST middleware (call early, before routing)
app.UseRest();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
Features
Correlation ID Propagation
- Prefers Kernel
IOperationContext.CorrelationIdif available (viaIOperationContextAccessor) - Falls back to
X-Correlation-Idheader from incoming requests - Generates a new correlation ID if not present
- Returns correlation ID in response headers
- Makes correlation ID available via
ICorrelationIdAccessor - Logs a warning when both Kernel and header correlation IDs are present but differ
Kernel Integration
When HoneyDrunk.Kernel is registered, the middleware automatically:
- Uses
IOperationContextAccessor.Current.CorrelationIdas the preferred correlation source - Enriches logging scopes with Kernel context values:
OperationId,OperationName,CausationIdTenantId,ProjectIdNodeId,StudioId,Environment(fromIGridContext)
Auth Integration
When HoneyDrunk.Auth.AspNetCore is registered:
- Uses
IAuthenticatedIdentityAccessorto provide context-aware error messages - Shapes 401/403 responses as
ApiErrorResponseviaIAuthorizationMiddlewareResultHandler - Falls back to
HttpContext.User.Identity.IsAuthenticatedwhenIAuthenticatedIdentityAccessoris not registered
Transport Integration
Extension methods for mapping ITransportEnvelope to ApiResult:
// From envelope
ApiResult result = envelope.ToApiResult();
ApiResult<T> result = envelope.ToApiResult(data);
// From outcome
ApiResult result = TransportResultExtensions.FromOutcome(isSuccess, errorMessage, correlationId);
Exception Mapping
All unhandled exceptions are mapped to ApiErrorResponse:
| Exception Type | HTTP Status | Error Code |
|---|---|---|
| JsonException | 400 | BAD_REQUEST |
| BadHttpRequestException | 400 | BAD_REQUEST |
| ArgumentException | 400 | BAD_REQUEST |
| Kernel ValidationException | 400 | BAD_REQUEST |
| InvalidOperationException | 409 | CONFLICT |
| Kernel ConcurrencyException | 409 | CONFLICT |
| KeyNotFoundException | 404 | NOT_FOUND |
| Kernel NotFoundException | 404 | NOT_FOUND |
| UnauthorizedAccessException | 403 | FORBIDDEN |
| Kernel SecurityException | 403 | FORBIDDEN |
| Kernel DependencyFailureException | 503 | SERVICE_UNAVAILABLE |
| NotImplementedException | 501 | NOT_IMPLEMENTED |
| OperationCanceledException | 499 | GENERAL_ERROR |
| Other | 500 | INTERNAL_ERROR |
Note: Kernel exceptions use safe static messages. The
HasStartedguard prevents errors when response streaming has begun.
Model Validation
Invalid model state is automatically converted to ApiErrorResponse with validation errors:
{
"correlationId": "abc-123",
"timestamp": "2026-01-10T00:00:00Z",
"error": {
"code": "VALIDATION_FAILED",
"message": "One or more validation errors occurred."
},
"validationErrors": [
{ "field": "email", "message": "Email is required." }
]
}
Minimal API Support
app.MapGet("/api/items/{id}", GetItemAsync)
.WithRest<ItemDto>();
app.MapPost("/api/items", CreateItemAsync)
.WithRestCreate<ItemDto>();
app.MapDelete("/api/items/{id}", DeleteItemAsync)
.WithRestDelete();
Configuration Options
| Option | Default | Description |
|---|---|---|
| CorrelationIdHeaderName | X-Correlation-Id | Header name for correlation ID |
| IncludeExceptionDetails | false | Include exception details in errors |
| IncludeTraceId | true | Include trace ID in responses |
| ReturnCorrelationIdInResponseHeader | true | Return correlation ID in headers |
| GenerateCorrelationIdIfMissing | true | Generate ID if not in request |
| EnableRequestLoggingScope | true | Enable logging scope middleware |
| EnableExceptionMapping | true | Enable exception mapping middleware |
| EnableModelStateValidationFilter | true | Enable model validation filter |
| ConfigureJsonDefaults | true | Configure JSON serialization |
| EnableAuthFailureShaping | true | Shape 401/403 as ApiErrorResponse |
Dependencies
- HoneyDrunk.Web.Rest.Abstractions (contracts)
- HoneyDrunk.Kernel.Abstractions 0.4.0 (optional, for Grid context and typed exceptions)
- HoneyDrunk.Auth.AspNetCore 0.2.0 (optional, for identity context)
- HoneyDrunk.Transport 0.4.0 (optional, for envelope mapping)
- Microsoft.AspNetCore.App (framework reference)
| 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
- HoneyDrunk.Auth.AspNetCore (>= 0.2.0)
- HoneyDrunk.Kernel.Abstractions (>= 0.4.0)
- HoneyDrunk.Transport (>= 0.4.0)
- HoneyDrunk.Web.Rest.Abstractions (>= 0.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v0.2.0: Added JsonException/BadHttpRequestException to 400 mapping, Kernel typed exception mappings (ValidationException, NotFoundException, ConcurrencyException, SecurityException, DependencyFailureException), HasStarted guard in ExceptionMappingMiddleware, correlation mismatch warning logging, and auth HttpContext.User fallback. Bumped Kernel.Abstractions to 0.4.0, Transport to 0.4.0, Auth.AspNetCore to 0.2.0.