CorrelationIdentifierMiddleware Class
Definition
- Namespace
- Cuemon.AspNetCore.Http.Headers
- Assemblies
- Cuemon.AspNetCore.dll
Provides a Correlation ID middleware implementation for ASP.NET Core.
public class CorrelationIdentifierMiddleware : ConfigurableMiddleware<CorrelationIdentifierOptions>, IConfigurable<CorrelationIdentifierOptions>
- Inheritance
-
CorrelationIdentifierMiddleware
- Implements
- Inherited Members
Examples
The following example demonstrates how to register CorrelationIdentifierMiddleware in the ASP.NET Core pipeline with a custom header name and correlation token. It then reads the correlation ID from an endpoint to confirm the middleware is working.
// Program.cs
using System;
using Cuemon.AspNetCore.Http.Headers;
using Cuemon.Messaging;
using Cuemon.Net.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddLogging();
services.Configure<CorrelationIdentifierOptions>(options =>
{
options.HeaderName = HttpHeaderNames.XCorrelationId;
options.Token = new CorrelationToken();
});
using var provider = services.BuildServiceProvider();
var app = new ApplicationBuilder(provider);
app.UseMiddleware<CorrelationIdentifierMiddleware>();
app.Run(async context =>
{
var correlationId = context.Items[CorrelationIdentifierMiddleware.HttpContextItemsKey];
await context.Response.WriteAsync($"Correlation ID: {correlationId}");
});
var pipeline = app.Build();
var httpContext = new DefaultHttpContext { RequestServices = provider };
await pipeline(httpContext);
Console.WriteLine(httpContext.Items[CorrelationIdentifierMiddleware.HttpContextItemsKey]);
Constructors
| Name | Description |
|---|---|
| CorrelationIdentifierMiddleware(RequestDelegate, IOptions<CorrelationIdentifierOptions>) | Initializes a new instance of the CorrelationIdentifierMiddleware class. |
| CorrelationIdentifierMiddleware(RequestDelegate, Action<CorrelationIdentifierOptions>) | Initializes a new instance of the CorrelationIdentifierMiddleware class. |
Fields
| Name | Description |
|---|---|
| HttpContextItemsKey | The key from where the Correlation ID is stored throughout the request scope. |
Methods
| Name | Description |
|---|---|
| InvokeAsync(HttpContext) | Executes the CorrelationIdentifierMiddleware. |