0

I am using the Enterprise Tester API to import and update content on the web application. The program will work fine for a couple of hours but occasionally it runs into this unhandled exception:

An unhandled exception of type 'System.Net.Http.HttpRequestException' occurred in EnterpriseTester.API.Client.dll

Additional information: System.Net.Http.HttpRequestException status code does not indicate success: 500 (Internal Server Error)

At this point, Visual Studio breaks at the line where this exception occurs. However, when I click "Continue", the program will execute properly again.

From searching on the internet, it seems that I should use a try catch block to handle the exception. I also want to be able to wait a little bit and execute the same line again to access the API.

    try
    {
        client.UpdateScriptRun(Id,Run);
    }
    catch (HttpRequestException e)
    {
        Console.Writeline("HttpRequestException: {0}", e.Message);
        Thread.Sleep(1000);
        client.UpdateScriptRun(Id,Run);
     }

I am not sure if something like this would solve the issue or if I need to look into a completely different solution.

It would be much appreciated if you could guide me to a solution that seems fit to this problem. Thank you!

Community
  • 1
  • 1
MShaw
  • 3
  • 3
  • Is the code you've posted above running in a WebApiController? Why would you want your POST to hang if the first call throws an exception? Why not return the 500 from your controller and then have your client try again? – maccettura Jun 20 '17 at 17:44
  • @maccettura Thanks for your question. The code above is from a Console application. The application is importing over a thousand documents/scripts on my desktop to Enterprise Tester. The "UpdateScriptRun" is one of their API methods that my program is calling to POST. If I return the 500, then client will have to run this program from the beginning again. What would you suggest as an alternate solution? – MShaw Jun 20 '17 at 18:17

1 Answers1

1

Usually it's best to track down the root cause of the exception, and see if there's a way to fix it. It's one thing if there's a connection error that just happens because the network is having intermittent issues, but a 500 error indicates that something went wrong on the server. If you have any way to track down and fix the server-side error, that's the best approach.

If you have no control over the server, and are just trying to make your own app work as well as possible knowing that the server occasionally fails for no reason, then a try/catch solution will help. I'd make a few additional recommendations, though:

  1. Use a more robust logging mechanism, so you can tell from your log files how often things are going wrong, and maybe use that information to track down issues. Include the parameters in your log message, in case that information turns out to be useful.
  2. Create a helper method to help you with your retries, so you can reuse the pattern elsewhere.
  3. In this helper method, start by retrying almost immediately, and then wait an exponentially increasing period of time (10ms, 100ms, 1000ms) until you either see success or hit a reasonable maximum limit.
  4. Whatever is consuming your code should have its own try/catch to ensure that the user has a decent experience when things aren't working right.
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Thank you for your answer. As I have no access to the server-side, your suggestion of a helper method sound promising. For this solution, should I have my helper method inside the "catch" block? Or are you suggesting having the try-catch inside the helper method. I apologize if my question seem trivial, I am not at all familiar this. Any useful links to related topics would also help. Thanks! – MShaw Jun 20 '17 at 18:32
  • Put the try/catch inside of a loop inside of your helper method, [kind of like this](https://stackoverflow.com/a/1563234/120955). Or you might find it worthwhile to use a library someone else has written, like [Polly](http://www.thepollyproject.org/2016/10/25/polly-5-0-a-wider-resilience-framework/). – StriplingWarrior Jun 21 '17 at 19:25