System.Net.Http.HttpClient 添加 Request-Id 标头

Posted

技术标签:

【中文标题】System.Net.Http.HttpClient 添加 Request-Id 标头【英文标题】:System.Net.Http.HttpClient adds Request-Id header 【发布时间】:2018-03-07 13:40:17 【问题描述】:

我注意到当HttpClient 在 ASP.Net Core web api 控制器中使用时,它会将Request-Id 标头添加到它发送的请求中。当HttpClient 用于例如.Net Core 控制台应用程序。

我认为这样做是为了实现关联(或跟踪)ID,但它是如何工作的?究竟是什么添加了这个标头?

另外,我该如何删除它?我已经实现了自己的相关 ID。

【问题讨论】:

应用洞察? @Tratcher 感谢您为我指明了正确的方向。现在我知道要谷歌什么了,我可以找到这个:blog.wille-zone.de/post/… 【参考方案1】:

当 RestSharp 发出的所有请求也包含 Request-Id 时,我偶然发现了这个问题。 solution mentioned in the comments 也为我工作,值得被提升为答案:

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);

【讨论】:

不幸的是,这在 .NET Core 3.1 中不起作用有人知道为什么吗? @PKCS12 你试过this suggestion 吗?它是最近的,我猜它适用于 ASP.NET Core 3.1 或更高版本。 我确实尝试过!它没有工作:

以上是关于System.Net.Http.HttpClient 添加 Request-Id 标头的主要内容,如果未能解决你的问题,请参考以下文章