Skip to content

Disabling activity trace propagation for a specific HttpClientHandler with underlying BrowserHttpHandler #85883

Open

Description

@uhfath

Seems there are mainly 2 methods for disabling trace propagation (header alteration) for a HttpClient:

  1. DistributedContextPropagator.Current = DistributedContextPropagator.CreateNoOutputPropagator() to make a process-wide change;
  2. new HttpClient(new SocketsHttpHandler { ActivityHeadersPropagator = null }); to make a specific SocketsHttpHandler change.

The problem is that when the project is used under a browser (in a Blazor WASM project), primary handler (which is by default HttpClientHandler) uses a BrowserHttpHandler as an underlying handler. And this handler doesn't have anything for propagator control. More than that it is marked as internal which makes in unavailable for a client.

But just like SocketsHttpHandler (which is created inside HttpClientHandler when not under browser platform) it also creates an underlying DiagnosticsHandler (also internal, btw). And I'm not sure there is any possibility to disable trace propagation for it except method (1).

The only solution I've came across currently is based on this gist. However I couldn't find a way to make it work for a specific HttpClientHandler or at least HttpClient.

This is need in a case where there are multiple typed HttpClient's which are accessing different API resources. And some of them are CORS limited which makes usage of custom headers problematic. But we don't want to loose tracing for other APIs.

Is there any workaround for .NET 6?
The only one I have in my mind right now is to duplicate BrowserHttpHandler in my app and 'reinvent' HttpClientHandler to use a custom one when under browser. But I'm not sure if it's the right path to choose.

Also I've found some discussions about DiagnosticHandler.SuppressActivityPropagation ( #29796 and #29291). Is this already implemented? Couldn't find anything.

And tried this. Didn't help either.

Activity

  1. ghost added
    untriaged
    on May 7, 2023
  2. radical commented on May 8, 2023

    @radical
    Member
  3. added
    arch-wasm
    on May 9, 2023
  4. MihaZupan commented on May 9, 2023

    @MihaZupan
    Member

    Workarounds are possible, but ideally, there would be a proper way to configure this via API.

    You can disable this entirely for the process by setting an AppContext switch

    AppContext.SetData("System.Net.Http.EnableActivityPropagation", false);

    or environment variable

    DOTNET_SYSTEM_NET_HTTP_ENABLEACTIVITYPROPAGATION = 0

    Or configure it per handler with a hacky workaround like this: https://gist.github.com/MihaZupan/835591bb22270b1aa7feeeece721520d
    which involves creating a custom propagator, and inserting a custom handler for clients where you want to disable trace propagation.

  5. uhfath commented on May 10, 2023

    @uhfath
    Author

    @MihaZupan thanks for the gist with another workaround. I was hoping that one of those mentioned enhancements were implemented to make a more granular control.

    Just in case if this needs more examples, we make requests for GitHub API under Blazor WASM. When activities are enabled (to track other requests) making a simple GET request under Chrome results in errors like this:
    image
    Stating that Request header field traceparent is not allowed by Access-Control-Allow-Headers in preflight response.

  6. added this to the Future milestone on May 10, 2023
  7. removed
    untriaged
    on May 10, 2023
  8. self-assigned this
    on May 10, 2023
  9. pavelsavara commented on Sep 17, 2025

    @pavelsavara
    Member

    I think we already implemented this with HttpActivityPropagationSupport and System.Net.Http.EnableActivityPropagation linker feature.

    Edit: you are asking about granular control

    <HttpActivityPropagationSupport Condition="'$(HttpActivityPropagationSupport)' == ''">false</HttpActivityPropagationSupport>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

arch-wasmarea-System.Net.Http

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions