Skip to content

Instantly share code, notes, and snippets.

@AnthonyGiretti
Created March 22, 2026 00:59
Show Gist options
  • Select an option

  • Save AnthonyGiretti/bcfb02187dbdbe7b48a0bf13f2de00c8 to your computer and use it in GitHub Desktop.

Select an option

Save AnthonyGiretti/bcfb02187dbdbe7b48a0bf13f2de00c8 to your computer and use it in GitHub Desktop.
C# 14 Interceptors - CorrelationMiddleware that sets the ambient value per request
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