W4k.AspNetCore.Correlator 3.3.0

dotnet add package W4k.AspNetCore.Correlator --version 3.3.0                
NuGet\Install-Package W4k.AspNetCore.Correlator -Version 3.3.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="W4k.AspNetCore.Correlator" Version="3.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add W4k.AspNetCore.Correlator --version 3.3.0                
#r "nuget: W4k.AspNetCore.Correlator, 3.3.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install W4k.AspNetCore.Correlator as a Cake Addin
#addin nuget:?package=W4k.AspNetCore.Correlator&version=3.3.0

// Install W4k.AspNetCore.Correlator as a Cake Tool
#tool nuget:?package=W4k.AspNetCore.Correlator&version=3.3.0                

W4k.AspNetCore.Correlator

W4k.AspNetCore.Correlator Build NuGet Version

Correlator helps you with handling correlation ID (also "request ID"): reading, generating new one and forwarding to subsequent requests.

Correlation ID is sent within HTTP headers. If header is not set, Correlator will happily generate new one for you.

Apart from accepting or generating correlation ID, it is also possible to return correlation ID back to caller, so in case when correlation ID is generated, caller is aware of that value.

To forward correlation ID to subsequent request, it is necessary to use designated HTTP message handler, see examples below.

W3 Trace Context and .NET

Be aware that Trace Context is not supported, Correlator helps you with simple non-standard headers.

Distributed tracing and trace context is built in .NET, You can get more insights from article: .NET distributed tracing.

Basic usage

Startup class

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDefaultCorrelator();

var app = builder.Build();

// register as first middleware
// (or as soon as possible to benefit from having correlation ID)
app.UseCorrelator();

// ...
app.Run();

Accessing correlation ID

Correlation ID of current request is available via ICorrelationContextAccessor.CorrelationContext:

using W4k.AspNetCore.Correlator;
using W4k.AspNetCore.Correlator.Context;

public class MyLittleService
{
    private readonly ICorrelationContextAccessor _contextAccessor;

    public MyLittleService(ICorrelationContextAccessor contextAccessor)
    {
        _contextAccessor = contextAccessor;
    }

    public async Task DoMagicalStuff()
    {
        CorrelationId correlationId = _contextAccessor.CorrelationContext.CorrelationId;

        // ...
    }
}

Forwarding correlation ID

In order to pass correlation ID to subsequent requests, additional HTTP message handler has to be registered.

Add CorrelatorHttpMessageHandler to HTTP client's message handler pipeline like this:

// named HTTP client
builder.Services
    .AddHttpClient("DummyClient")
    .WithCorrelation();

// typed HTTP client
builder.Services
    .AddHttpClient<FooClient>()
    .WithCorrelation();

// registering HTTP message handler manually
builder.Services
    .AddHttpClient("FizzClient")
    .AddHttpMessageHandler<CorrelatorHttpMessageHandler>();

// registering HTTP client with custom settings
// (global options - CorrelatorOptions.Forward - won't be used)
builder.Services
    .AddHttpClient<LegacyClient>()
    .WithCorrelation(PropagationSettings.PropagateAs("X-Legacy-Correlation-Id"));

// ...

See "Configure the HttpMessageHandler" for more details about usage of HTTP message handler.

Validation of correlation ID

It is possible to validate correlation ID value and prevent invalid value to spread. By default, validation is turned off. In order to turn validation on, implementation of ICorrelationValidator has to be registered.

Correlator is shipped with lightweight validator, CorrelationValueLengthValidator, which decides whether received value is valid simply based on its length.

builder.Services
    .AddDefaultCorrelator()
    .WithValidator(new CorrelationValueLengthValidator(64));

Documentation


Icon made by Kiranshastry from Flaticon, www.flaticon.com

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.3.0 17 11/23/2024
3.2.0 427 1/15/2024
3.1.0 242 11/27/2023
3.0.0 119 11/18/2023
2.3.0 223 8/28/2023
2.2.2 1,816 7/25/2022
2.2.1 9,797 9/8/2020
2.2.0 454 9/6/2020
2.2.0-preview2 362 9/5/2020
2.2.0-preview1 358 9/4/2020
2.1.1 459 9/4/2020
2.0.1 447 8/31/2020
2.0.1-beta 407 8/30/2020
2.0.0-beta 290 8/27/2020
1.1.0 7,200 9/26/2019
1.0.1 1,992 6/27/2019
1.0.0 1,356 10/22/2018
0.2.0-alpha 583 9/28/2018
0.1.0-alpha 587 9/27/2018