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
<PackageReference Include="W4k.AspNetCore.Correlator" Version="3.3.0" />
paket add W4k.AspNetCore.Correlator --version 3.3.0
#r "nuget: W4k.AspNetCore.Correlator, 3.3.0"
// 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
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
- Detailed configuration description
- Dependency injection registration
- Components description
- Alternative packages and further reading
Icon made by Kiranshastry from Flaticon, www.flaticon.com
Product | Versions 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. |
-
net8.0
- Microsoft.Extensions.Http (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
- Microsoft.Extensions.Options.DataAnnotations (>= 8.0.0)
-
net9.0
- Microsoft.Extensions.Http (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.DataAnnotations (>= 9.0.0)
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 | 92 | 11/23/2024 |
3.2.0 | 449 | 1/15/2024 |
3.1.0 | 252 | 11/27/2023 |
3.0.0 | 123 | 11/18/2023 |
2.3.0 | 227 | 8/28/2023 |
2.2.2 | 1,835 | 7/25/2022 |
2.2.1 | 9,809 | 9/8/2020 |
2.2.0 | 460 | 9/6/2020 |
2.2.0-preview2 | 364 | 9/5/2020 |
2.2.0-preview1 | 360 | 9/4/2020 |
2.1.1 | 466 | 9/4/2020 |
2.0.1 | 453 | 8/31/2020 |
2.0.1-beta | 410 | 8/30/2020 |
2.0.0-beta | 292 | 8/27/2020 |
1.1.0 | 7,212 | 9/26/2019 |
1.0.1 | 1,998 | 6/27/2019 |
1.0.0 | 1,367 | 10/22/2018 |
0.2.0-alpha | 591 | 9/28/2018 |
0.1.0-alpha | 594 | 9/27/2018 |