簡體   English   中英

.NET HttpClient 在 Azure WebApp 中緩慢或無響應

[英].NET HttpClient slow or not responding in Azure WebApp

我們正在 Azure 應用服務上運行 WebJob 和 Api。 一些 Web 作業對第三方服務(如 ebay)執行 REST 調用。 一切正常,直到幾天前,服務開始隨機拋出此錯誤:

{\"ClassName\":\"System.Net.Http.HttpRequestException\",\"Message\":\"An error occurred while sending the request.\",\"Data\":{},\"InnerException\":{\"ClassName\":\"System.Net.WebException\",\"Message\":\"The underlying connection was closed: An unexpected error occurred on a receive.\",\"Data\":{},\"InnerException\":{\"ClassName\":\"System.IO.IOException\",\"Message\":\"Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.\",\"Data\":{},\"InnerException\":{\"NativeErrorCode\":10060,\"ClassName\":\"System.Net.Sockets.SocketException\",\"Message\":\"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond\",\"Data\":{},\"InnerException\":null,\"HelpURL\":null,\"StackTraceString\":\"   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)\\r\\n   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)\",\"RemoteStackTraceString\":null,\"RemoteStackIndex\":0,\"ExceptionMethod\":\"8\\nEndReceive\\nSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\\nSystem.Net.Sockets.Socket\\nInt32 EndReceive(System.IAsyncResult)\",\"HResult\":-2147467259,\"Source\":\"System\",\"WatsonBuckets\":null},\"HelpURL\":null,\"StackTraceString\":\"   at System.Net.Security._SslStream.EndRead(IAsyncResult asyncResult)\\r\\n   at System.Net.TlsStream.EndRead(IAsyncResult asyncResult)\\r\\n   at System.Net.Connection.ReadCallback(IAsyncResult asyncResult)\",\"RemoteStackTraceString\":null,\"RemoteStackIndex\":0,\"ExceptionMethod\":\"8\\nEndRead\\nSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\\nSystem.Net.Security._SslStream\\nInt32 EndRead(System.IAsyncResult)\",\"HResult\":-2146232800,\"Source\":\"System\",\"WatsonBuckets\":null}

調用有時有效,但速度很慢,有時會返回錯誤。 運行服務的本地實例不會導致失敗。 只有在生產環境中,我們才會有這些問題。

我們使用 HttpClient 的單例實例來執行調用。

public sealed class Client : HttpClient
    {
        private static volatile Client _instance = new Client();

        static Client()
        {
        }

        private Client() : base(new NativeMessageHandler())
        {
            // limit the connections in parallel to 100 by default
            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            // if this setting does not work, follow these instructions on app.config
            // ServicePointManager.DefaultConnectionLimit needs to be set before the ServicePoint is created 
            ServicePointManager.DefaultConnectionLimit = 100;
        }

        public static Client Instance => _instance;
    }

我們像這樣使用客戶端調用端點:

var client = Client.Instance;
var authenticationHeader = new AuthenticationHeaderValue("Bearer", token.AuthToken);
var url = "https://api.ebay.com/sell/account/v1/fulfillment_policy?marketplace_id=EBAY_DE";
var response = await client.GetMessageAsync(url, m => m.Headers.Authorization = authenticationHeader);

GetMessageAsync 方法是一個擴展方法,只是執行設置標頭的操作。

微軟宣布此安全補丁后不久問題就開始了: https : //docs.microsoft.com/answers/questions/6842/announcement-samesite-cookie-handling-and-net-fram.html

客戶端設置為接受 TLS 1.2 和 1.1。

在縮小可能的原因方面,我讓 ebay 和 Microsoft 支持檢查他們的系統。 事實證明,這實際上是 ebay 方面的一個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM