locked
Handling Time-Outs In API Request RRS feed

  • Question

  • User-1056855865 posted

    How do you handle a time-out in an API request?  I keep getting this error 
    System.Net.WebException: The operation has timed out

    1) What can I do, or should I do, to prevent this error

    2) When this error is thrown, what do I do to continue processing and not have the entire routine killed?

    Friday, March 1, 2019 1:46 PM

Answers

  • User475983607 posted

    ManderinViolin12

    It's an issue with the actual API itself.  It's a 3rd party API that we have no control over and it will randomly just throw a time-out error when we try to query data from it.

    The most logical course of action is contacting the 3rd party and provide them with code that reproduces the issue.

    It is also possible that there are bugs in your code base.  For example, connection exhaustion by not closing connections properly.

    Keep in mind, we cannot see your code or reproduce this issue.  It is up to you to troubleshoot.   I would start with the 3rd party if you feel your code is not the problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 1, 2019 3:09 PM

All replies

  • User475983607 posted

    1) What can I do, or should I do, to prevent this error

    There's no magic... this is purely troubleshooting/analysis.  First figure out what is causing the time and try to optimize the related code.  Commonly, the database tables are missing indexes.  If the code is optimized then maybe increase the timeout or do a fire and forget.

    http://blog.stephencleary.com/2014/06/fire-and-forget-on-asp-net.html

    2) When this error is thrown, what do I do to continue processing and not have the entire routine killed?

    Use proper exception handling; try...catch.

    Friday, March 1, 2019 2:34 PM
  • User-1056855865 posted

    It's an issue with the actual API itself.  It's a 3rd party API that we have no control over and it will randomly just throw a time-out error when we try to query data from it.

    Friday, March 1, 2019 2:36 PM
  • User475983607 posted

    ManderinViolin12

    It's an issue with the actual API itself.  It's a 3rd party API that we have no control over and it will randomly just throw a time-out error when we try to query data from it.

    The most logical course of action is contacting the 3rd party and provide them with code that reproduces the issue.

    It is also possible that there are bugs in your code base.  For example, connection exhaustion by not closing connections properly.

    Keep in mind, we cannot see your code or reproduce this issue.  It is up to you to troubleshoot.   I would start with the 3rd party if you feel your code is not the problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 1, 2019 3:09 PM
  • User303363814 posted

    Maybe you are trying to do something that the third party does not expect you to do.

    For example, a hypothetical web based api returns the available flights between two airports for a range of dates to support holiday planning.  The api works fine for normal use.  Your query asks for all flights between New York and Washington from 1/1/1900 to 1/1/2100 because you are investigating the history of flight in the US.  This would be way more data than the api designers planned for.  The query on the web site may be getting rental car offers, hotel availability and all sorts of other stuff which makes sense for holiday planning but just turns your request into a truly enormous load which would take minutes to complete if allowed to run.

    As always … what are you really trying to do?

    Friday, March 1, 2019 10:30 PM