.NET HttpClient 在 Azure WebApp 中响应缓慢或没有响应
.NET HttpClient slow or not responding in Azure WebApp
我们是 运行 WebJob 和 Api Azure App Services。一些 WebJobs 执行对第三方服务的 REST 调用,例如 ebay。一切正常,直到几天前,当服务开始随机抛出此错误时:
{\"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}
调用有时有效,但速度很慢,有时 return 错误。 运行 服务的本地实例不会导致失败。只有在生产环境中,我们才会有这些问题。
我们使用 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 方法是一种扩展方法,仅执行设置 header 的操作。
问题是在 Microsoft 发布此安全补丁后不久出现的:https://docs.microsoft.com/answers/questions/6842/announcement-samesite-cookie-handling-and-net-fram.html
客户端设置为接受 TLS 1.2 和 1.1。
为了缩小可能的原因,我让 ebay 和 Microsoft 支持人员检查了他们的系统。事实证明,这实际上是易趣方面的一个问题。
我们是 运行 WebJob 和 Api Azure App Services。一些 WebJobs 执行对第三方服务的 REST 调用,例如 ebay。一切正常,直到几天前,当服务开始随机抛出此错误时:
{\"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}
调用有时有效,但速度很慢,有时 return 错误。 运行 服务的本地实例不会导致失败。只有在生产环境中,我们才会有这些问题。
我们使用 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 方法是一种扩展方法,仅执行设置 header 的操作。
问题是在 Microsoft 发布此安全补丁后不久出现的:https://docs.microsoft.com/answers/questions/6842/announcement-samesite-cookie-handling-and-net-fram.html
客户端设置为接受 TLS 1.2 和 1.1。
为了缩小可能的原因,我让 ebay 和 Microsoft 支持人员检查了他们的系统。事实证明,这实际上是易趣方面的一个问题。