Vignesh Kumar A
Vignesh Kumar A

Reputation: 28413

API Exception randomly : A connection attempt failed because the connected party did not properly respond after a period of time

I have hosted my application in prod server. This application will hit the Bing map concurrently to bring the location based response. This application is used to send N number of location as API request to get the API response in order to process the location data.

API : "https://dev.virtualearth.net/REST/v1/Locations"

And its working fine but randomly getting following exception, which impacts the application consistent.

API Call

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;
    }
}

Exception

{
    "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
}

Upvotes: 1

Views: 4254

Answers (1)

Ponmani Chinnaswamy
Ponmani Chinnaswamy

Reputation: 861

For your WSAETIMEDOUT 10060 error, Try these solutions.

  1. Set the Timeout and ReadWriteTimeout for HttpWebRequest.

Adjusting HttpWebRequest Connection Timeout in C#

  1. HttpWebResponse code in using block.

HttpWebRequest.GetResponse() keeps getting timed out

Upvotes: -1

Related Questions