Search code examples
c#http-headers

Remove TraceParent header from HttpClient requests


When I send any requests with HttpClient, it automatically attaches the header TraceParent to the request headers. I tried clearing all the default request headers, but that didn't make a difference.


Solution

  • Header traceparent or Request-Id specified by DiagnosticsHandler: https://github.com/dotnet/runtime/blob/release/5.0/src/libraries/System.Net.Http/src/System/Net/Http/DiagnosticsHandler.cs#L286

    How to disable this behavior is described here: https://github.com/dotnet/aspnetcore/issues/21012#issuecomment-616570453

    ASP.NET Core creates an Activity that represents the request by default. This is what tells HttpClient to send an outgoing request id header. You can disable it in a couple of ways:

    • By setting the current activity to null before making the request (Activity.Current = null)
    • By setting the environment variable DOTNET_SYSTEM_NET_HTTP_ENABLEACTIVITYPROPAGATION to "false" or "0".
    • By the AppContext switch System.Net.Http.EnableActivityPropagation to false