Created
March 22, 2026 00:59
-
-
Save AnthonyGiretti/bcfb02187dbdbe7b48a0bf13f2de00c8 to your computer and use it in GitHub Desktop.
C# 14 Interceptors - CorrelationMiddleware that sets the ambient value per request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace MyApi; | |
| public class CorrelationMiddleware(RequestDelegate next) | |
| { | |
| public async Task InvokeAsync(HttpContext context) | |
| { | |
| CorrelationContext.CorrelationId = | |
| context.Request.Headers["X-Correlation-ID"].FirstOrDefault() | |
| ?? Guid.NewGuid().ToString(); | |
| await next(context); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment