Table of Contents

Class CorrelationIdentifierMiddleware

Namespace
Cuemon.AspNetCore.Http.Headers
Assembly
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.

using System.Threading.Tasks;
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;

namespace MyApp.Http.Headers
{
    public class CorrelationIdentifierMiddlewareRegistration
    {
        // Called from Startup.ConfigureServices or Program.cs
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CorrelationIdentifierOptions>(o =>
            {
                // Use a custom header name
                o.HeaderName = HttpHeaderNames.XCorrelationId;
                // Provide a specific correlation token
                o.Token = new CorrelationToken();
            });
        }

        // Called from Startup.Configure or Program.cs
        public void Configure(IApplicationBuilder app)
        {
            // Add the Correlation ID middleware to the pipeline
            app.UseMiddleware<CorrelationIdentifierMiddleware>();

            // Example endpoint that reads the correlation ID
            app.Run(async context =>
            {
                var correlationId = context.Items[CorrelationIdentifierMiddleware.HttpContextItemsKey];
                await context.Response.WriteAsync(
                    $"Correlation ID: {correlationId}");
            });
        }
    }
}

Constructors

CorrelationIdentifierMiddleware(RequestDelegate, IOptions<CorrelationIdentifierOptions>)

Initializes a new instance of the CorrelationIdentifierMiddleware class.

public CorrelationIdentifierMiddleware(RequestDelegate next, IOptions<CorrelationIdentifierOptions> setup)

Parameters

next RequestDelegate

The delegate of the request pipeline to invoke.

setup IOptions<CorrelationIdentifierOptions>

The CorrelationIdentifierOptions which need to be configured.

CorrelationIdentifierMiddleware(RequestDelegate, Action<CorrelationIdentifierOptions>)

Initializes a new instance of the CorrelationIdentifierMiddleware class.

public CorrelationIdentifierMiddleware(RequestDelegate next, Action<CorrelationIdentifierOptions> setup)

Parameters

next RequestDelegate

The delegate of the request pipeline to invoke.

setup Action<CorrelationIdentifierOptions>

The CorrelationIdentifierOptions which need to be configured.

Fields

HttpContextItemsKey

The key from where the Correlation ID is stored throughout the request scope.

public const string HttpContextItemsKey = "Cuemon.AspNetCore.Http.Headers.CorrelationIdentifierMiddleware"

Field Value

string

Methods

InvokeAsync(HttpContext)

public override Task InvokeAsync(HttpContext context)

Parameters

context HttpContext

The context of the current request.

Returns

Task

A task that represents the execution of this middleware.