Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Net.Http.HttpClient添加请求Id标头_C#_Asp.net Core_Dotnet Httpclient - Fatal编程技术网

C# System.Net.Http.HttpClient添加请求Id标头

C# System.Net.Http.HttpClient添加请求Id标头,c#,asp.net-core,dotnet-httpclient,C#,Asp.net Core,Dotnet Httpclient,我注意到,在ASP.Net核心web api控制器中使用HttpClient时,它会将Request Id头添加到它发送的请求中。如果在.Net核心控制台应用程序中使用HttpClient,则不会发生这种情况 我假定这样做是为了实现相关(或跟踪)ID,但它是如何工作的?到底是什么添加了此标题 还有,我怎样才能删除它?我已经实现了自己的关联Id。当RestSharp发出的所有请求也包含请求Id时,我偶然发现了这个问题。这也对我起了作用,值得作为一个答案加以推广: Startup.cs privat

我注意到,在ASP.Net核心web api控制器中使用
HttpClient
时,它会将
Request Id
头添加到它发送的请求中。如果在.Net核心控制台应用程序中使用
HttpClient
,则不会发生这种情况

我假定这样做是为了实现相关(或跟踪)ID,但它是如何工作的?到底是什么添加了此标题


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

当RestSharp发出的所有请求也包含
请求Id时,我偶然发现了这个问题。这也对我起了作用,值得作为一个答案加以推广:

Startup.cs
private void disableCorrelationId(IServiceCollection服务)
{
var module=services.FirstOrDefault(t=>
t、 实现工厂?.GetType()==typeof(Func));
如果(模块!=null)
{
服务。移除(模块);
services.AddSingleton(provider=>newDependencyTrackingTeleMetryModule
{
SetComponentCorrelationHttpHeaders=false
});
}
}
public void配置服务(IServiceCollection服务)
{
//其他东西可能会来这里
禁用相关ID(服务);
}

Application Insights?@Tratcher感谢您为我指明了正确的方向。现在,当我知道谷歌应该做什么时,我发现了这个:不幸的是,这在.NET Core 3.1中不起作用。有人知道为什么吗?@PKCS12你试过了吗?这是最近发布的,我想是针对ASP.NETCore3.1或更高版本的。我确实试过了!它不起作用:
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);
}