Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Net.Http.HttpClient adds Request-Id header

I noticed that when HttpClient is used in ASP.Net Core web api controller, it adds Request-Id header to the requests it sends. That does not happen when HttpClient is used in e.g. a .Net Core console app.

I presume this is done to implement correlation (or tracking) IDs, but how does it work? What exactly adds this header?

Also how can I remove it? I've implemented my own correlation IDs.

like image 805
Andrew Avatar asked Apr 07 '26 03:04

Andrew


1 Answers

I have stumbled across this problem when all requests issued by RestSharp were also containing Request-Id. The solution mentioned in the comments also worked for me and deserves to be promoted as an answer:

Startup.cs

private void DisableCorrelationIds(IServiceCollection services)
{
    var module = services.FirstOrDefault(t =>
        t.ImplementationFactory?.GetType() == typeof(Func<IServiceProvider, DependencyTrackingTelemetryModule>));
    if (module != null)
    {
        services.Remove(module);
        services.AddSingleton<ITelemetryModule>(provider => new DependencyTrackingTelemetryModule
        {
            SetComponentCorrelationHttpHeaders = false
        });
    }
}

public void ConfigureServices(IServiceCollection services)
{
    // other stuff may come here
    DisableCorrelationIds(services);
}
like image 78
Alexei - check Codidact Avatar answered Apr 09 '26 15:04

Alexei - check Codidact