System.Net.Http.HttpClient 添加 Request-Id header

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

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

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

还有我怎样才能删除它?我已经实现了自己的关联 ID。

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