W4k.AspNetCore.Correlator 3.2.0

dotnet add package W4k.AspNetCore.Correlator --version 3.2.0
NuGet\Install-Package W4k.AspNetCore.Correlator -Version 3.2.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.2.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.2.0
#r "nuget: W4k.AspNetCore.Correlator, 3.2.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.2.0

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

W4k.AspNetCore.Correlator

W4k.AspNetCore.Correlator Build NuGet Badge CodeQL

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 of 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

public class MyLittleStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDefaultCorrelator();
    }

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

        app.UseMvc();
    }
}

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:

public class MyLittleStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // named HTTP client
        services
            .AddHttpClient("DummyClient")
            .WithCorrelation();

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

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

        // registering HTTP client with custom settings
        // (global options - CorrelatorOptions.Forward - won't be used)
        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.

public void ConfigureServices(IServiceCollection services)
{
    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 net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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. 
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.2.0 79 1/15/2024
3.1.0 159 11/27/2023
3.0.0 92 11/18/2023
2.3.0 194 8/28/2023
2.2.2 1,649 7/25/2022
2.2.1 9,320 9/8/2020
2.2.0 440 9/6/2020
2.2.0-preview2 344 9/5/2020
2.2.0-preview1 335 9/4/2020
2.1.1 431 9/4/2020
2.0.1 427 8/31/2020
2.0.1-beta 362 8/30/2020
2.0.0-beta 274 8/27/2020
1.1.0 7,160 9/26/2019
1.0.1 1,966 6/27/2019
1.0.0 1,327 10/22/2018
0.2.0-alpha 559 9/28/2018
0.1.0-alpha 556 9/27/2018