API随机异常:连接尝试失败,因为连接方在一段时间后没有正确响应

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

我已将我的应用程序托管在产品服务器中。该应用程序将同时访问 Bing 地图以带来基于位置的响应。该应用程序用于发送 N 个位置作为 API 请求以获取 API 响应以处理位置数据。

API:“https://dev.virtualearth.net/REST/v1/Locations”

它工作正常,但随机出现以下异常,这影响了应用程序的一致性。

API调用

private async Task<BingResponse> GetBingResponse(string argUri)
{
    try
    {
        WebRequest request = HttpWebRequest.Create(new Uri(argUri));
        var response = await request.GetResponseAsync();

        if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK)
        {
            using (Stream resStream = (response.GetResponseStream()))
            {
                DataContractJsonSerializer jsonData = new DataContractJsonSerializer(typeof(BingResponse));
                return jsonData.ReadObject(resStream) as BingResponse;
            }
        }
        else
            return null;
    }
    catch (Exception ex)
    {
        XLLogger.Write("Exception with BingResponse URL");
        return null;
    }
}

异常

{
    "ClassName": "System.Net.WebException",
    "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. (dev.virtualearth.net:443)",
    "Data": null,
    "InnerException": {
        "StatusCode": null,
        "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. (dev.virtualearth.net:443)",
        "Data": {},
        "InnerException": {
            "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": null,
            "InnerException": null,
            "HelpURL": null,
            "StackTraceString": "   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)\r\n   at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|277_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)\r\n   at System.Net.HttpWebRequest.<>c__DisplayClass216_0.<<CreateHttpClient>b__1>d.MoveNext()\r\n--- End of stack trace from previous location ---\r\n   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)",
            "RemoteStackTraceString": null,
            "RemoteStackIndex": 0,
            "ExceptionMethod": null,
            "HResult": -2147467259,
            "Source": "System.Net.Sockets",
            "WatsonBuckets": null,
            "NativeErrorCode": 10060
        },
        "HelpLink": null,
        "Source": "System.Net.Http",
        "HResult": -2147467259,
        "StackTrace": "   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(HttpRequestMessage request)\r\n   at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpConnectionPool.GetHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)\r\n   at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\r\n   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)\r\n   at System.Net.HttpWebRequest.SendRequest(Boolean async)\r\n   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)"
    },
    "HelpURL": null,
    "StackTraceString": "   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)\r\n   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)\r\n--- End of stack trace from previous location ---\r\n",
    "RemoteStackTraceString": null,
    "RemoteStackIndex": 0,
    "ExceptionMethod": null,
    "HResult": -2147467259,
    "Source": "System.Net.Requests",
    "WatsonBuckets": null
}
c# .net-core asp.net-core-webapi bing-api
1个回答
0
投票

对于您的 WSAETIMEDOUT 10060 错误,请尝试此解决方案。

  1. 设置 HttpWebRequest 的超时和 ReadWriteTimeout。

参考 在 C# 中调整 HttpWebRequest 连接超时

© www.soinside.com 2019 - 2024. All rights reserved.