System.Net.Http.HttpClient添加了Request-Id头部

3
我注意到在ASP.Net Core web api控制器中使用HttpClient时,它会向发送的请求中添加Request-Id头部。但是,在例如.Net Core控制台应用程序中使用HttpClient时,不会发生这种情况。
我猜测这是为了实现关联(或跟踪)ID,但是它是如何工作的?是什么确切地添加了这个标头?
另外,我该如何删除它?我已经实现了自己的关联ID。

1
应用洞察? - Tratcher
@Tratcher 感谢您指引我正确的方向。现在我知道该搜索什么,我成功找到了这篇文章:https://blog.wille-zone.de/post/disable-application-insights-correlation-id-headers-on-httpclient-requests-in-aspnet-core/ - Andrew
1个回答

3

当我使用RestSharp发出的所有请求都包含Request-Id时,我遇到了这个问题。在评论中提到的解决方案也适用于我,并值得作为答案推广:

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
1
@PKCS12 你尝试过这个建议了吗?它是最近的,我猜它是针对ASP.NET Core 3.1或更高版本的。 - Alexei - check Codidact
我确实尝试过那个!但它没有起作用 :< - PKCS12

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接