如何从 C# 中的传出请求中删除 HTTP 标头

问题描述 投票:0回答:1

我有一个 C# .net core 6 中的 API 项目,托管在 k8 中。 我在我的项目中启用了 new relic 并跟踪每个 API 请求,如下所示。

 Uri uri = new Uri($"{uriHostPath}inittrans");
 NewRelic.Api.Agent.NewRelic.SetTransactionUri(uri);

在我的应用程序中,我正在调用外部 SOAP 服务。 现在的问题是很少有外部请求由于超时而失败。 从对外服务的角度来看,下游的soap服务没有问题。 为了调试超时问题,我们提取了传出请求的 TCP 转储,发现每个传出 HTTP 请求中都添加了一个带有大字符串值(base64 编码字符串)的 newrelic 标头,这导致了网络问题。 TCP 转储负载示例:

010.028.025.228.58658-202.043.122.108.00080: POST /ticketbook/clsBook.asmx HTTP/1.1
Host: ****
Cache-Control: no-cache, max-age=0
SOAPAction: "http://www.gtree.co.in/objExecute"

newrelic: eyJ2Ijpb..............OWUzNzlhYmNmNmUifX0=

traceparent: 00-1b88a00531a7f2a46b45236b531db35b-94a1c9e379abcf6e-01
tracestate: 206830@nr=0-0-206830-1078067976-94a1c9e379abcf6e-974e420a2ec4ad63-1-1.322319-1715076143685,
elastic-apm-traceparent: 00-ef11ab2c795d174b908650ea571bfd42-1b738c70ab52be49-00
Content-Type: text/xml; charset=utf-8
Content-Length: 447

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><objExecute xmlns="http://www.gtree.co.in" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><strCommand>GETDATA</strCommand><strParam1>123</strParam1><strParam2/><strParam3/><strParam4/><strParam5/><strParam6/><strParam7/><strParam8/><strParam9/><strParam10/><strParam11/><strParam12/><strParam13/><strParam14/><strParam15/></objExecute></s:Body></s:Envelope>
********: HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Length: 537
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 07 May 2024 10:00:41 GMT

此后,我们在应用程序中禁用了新的遗物代理,然后问题得到解决。 我们不想停止对我们的应用程序的监控,因此删除 newrelic 代理是对我们有用的临时解决方案。

我尝试借助 CustomEndpointBehaviour 类的代码从传出 HTTP 请求中删除 newrelic 标头,但这也不起作用。

    public class CustomEndpointBehaviour : IEndpointBehavior
    {
        private readonly Func<HttpMessageHandler> _httpMessageHandler;

        public CustomEndpointBehaviour(IHttpMessageHandlerFactory factory)
        {
            // Here we prescribe how handler will be created.
            // Since it uses IHttpMessageHandlerFactory, this factory will manage the setup and lifetime of the handler, 
            // based on the configuration we provided with AddHttpClient(serviceName) 
            _httpMessageHandler = () => factory.CreateHandler();
        }

        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            // We need this line to add our HttpMessageHandler as HttpClientHandler.
            bindingParameters?.Add(new Func<HttpClientHandler, HttpMessageHandler>(handler => _httpMessageHandler()));
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            // Add custom message inspector to remove New Relic header
            clientRuntime.ClientMessageInspectors.Add(new CustomMessageInspector());
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }

        public void Validate(ServiceEndpoint endpoint) { }

    }

    public class CustomMessageInspector : IClientMessageInspector
    {
        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
        {
            // Find and remove the New Relic header
            var headers = request.Headers;
            if (headers.FindHeader("newrelic", "") != -1)
            {
                headers.RemoveAt(headers.FindHeader("newrelic", ""));
            }

            return null;
        }

        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState) { }
    }
}

有什么方法可以告诉新的遗物代理不要为传出请求添加任何标头吗? 还有什么方法可以告诉 .net core 服务器从其中排除某些标头 传出的 http 请求。

c# http .net-core soap newrelic
1个回答
0
投票
It seems like you're dealing with issues related to New Relic headers causing problems with outgoing HTTP requests from your .NET Core application. While it's crucial to maintain monitoring for your application, you also need to ensure that these headers don't interfere with external services.

Here are some suggestions to address this:

Contact New Relic Support: Reach out to New Relic's support team. They might provide specific guidance or updates regarding this issue. New Relic often releases updates and patches, so there might be a newer version or configuration setting that addresses this problem.
Custom Header Configuration: Check if New Relic provides any configuration options to exclude certain headers or disable header injection for outgoing requests. New Relic's documentation or support channels should provide information on any available settings for this.
Modify HTTP Client Behavior: Your attempt to remove the New Relic header using a custom HttpClientHandler and message inspector seems reasonable. However, you might need to ensure that this behavior is applied to all outgoing HTTP requests, including those made by libraries or frameworks you're using.Double-check that your custom behavior is correctly registered and applied to all outgoing requests. Also, ensure that there are no other interceptors or middleware in your application that might add these headers back after your custom behavior removes them.
Network Proxy or Gateway Configuration: If you're using a network proxy or gateway (e.g., NGINX, HAProxy) between your application and external services, you might be able to configure it to strip certain headers before forwarding requests. This approach can be useful if modifying application code isn't feasible or effective.
Temporary Solutions: If you can't find a permanent solution immediately, you might need to continue with the temporary workaround of disabling New Relic monitoring until a better solution is implemented. However, keep monitoring New Relic's updates and announcements for any fixes or improvements related to this issue.
Remember to thoroughly test any changes in a development or staging environment before applying them to production to ensure they don't have unintended consequences on your application or its interactions with external services.
© www.soinside.com 2019 - 2024. All rights reserved.