0

I'm configuring IP cameras with c# .NET. I'm applying a new firmware which reboots the camera. Post-reboot, the camera actively rejects connections for a time, then goes to 503-service unavailable for a bit more time.

I'm looking for a method that will continually probe the camera, ignoring AggregateExceptions and 503's until it returns 200/OK. Then continue with the configuration process.

This is the code I have thus far, and it catches the exceptions, but again I want it to keep trying until successful.

        var statusDic = new Dictionary<HttpStatusCode, string>();

        try
        {
            using (var client = new HttpClient(new HttpClientHandler
            {
                AutomaticDecompression = DecompressionMethods.GZip,
                Credentials = new NetworkCredential("root", "pass")
            }))
            {
                client.BaseAddress = new Uri($@"http://{dicPair.Value}/axis-cgi/param.cgi");
                var response = client.GetAsync(Source.config);

                while (response.Exception != null || response.Result.StatusCode != HttpStatusCode.OK)
                {
                    Thread.Sleep(5000);
                    response = client.GetAsync(Source.config);
                }

                statusDic.Add(response.Result.StatusCode, response.Result.ToString());
            }
        }
        catch (WebException ex)
        {
            using (var sr =
                new StreamReader(ex.Response.GetResponseStream() ?? throw new InvalidOperationException()))
            {
                Logger.LogError(sr.ReadToEnd());
            }
        }
        catch (AggregateException ex)
        {
            Logger.LogError(ex.Message);
        }

        return statusDic;
Drew Jackson
  • 9
  • 1
  • 13

2 Answers2

1

Having similar cases iIn our projects, we use Polly library https://github.com/App-vNext/Polly

It is very flexible, rule-based, NuGet-available, BSD-licensed and so on. Given that, you don't need to re-invent the bicycle.

You can check this nice post by Scott Hanselman about real-world Polly usage: https://www.hanselman.com/blog/AddingResilienceAndTransientFaultHandlingToYourNETCoreHttpClientWithPolly.aspx

Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42
0

This is what the code looks like now (thanks to several people for the ideas):

var statusDic = new Dictionary<HttpStatusCode, string>();
        Task<HttpResponseMessage> response;
        var client = new HttpClient(new HttpClientHandler
        {
            AutomaticDecompression = DecompressionMethods.GZip,
            Credentials = new NetworkCredential("root", "pass")
        });

        try
        {
            client.BaseAddress = new Uri($@"http://{dicPair.Value}/axis-cgi/param.cgi");

            while (true)
            {
                response = client.GetAsync(Source.config);
                if (response.Result.IsSuccessStatusCode)
                    break;
                Task.Delay(5000);
            }

            statusDic.Add(response.Result.StatusCode, response.Result.ToString());
        }
        catch (WebException ex)
        {
            using (var sr =
                new StreamReader(ex.Response.GetResponseStream() ?? throw new InvalidOperationException()))
            {
                Logger.LogError($@"WEB EXCEPTION - {sr.ReadToEnd()}");
                statusDic.Add(HttpStatusCode.BadRequest, sr.ReadToEnd());
            }
        }
        catch (AggregateException ex)
        {
            Logger.LogDebug($@"AGGREGATE EXCEPTION - {ex.InnerExceptions}");
            while (true)
            {
                response = client.GetAsync(Source.config);
                if (response.Result.IsSuccessStatusCode)
                    break;
                Task.Delay(5000);
            }

            statusDic.Add(response.Result.StatusCode, response.Result.ToString());
        }

        return statusDic;

And so far, this seems to be working. Thanks everyone!

Drew Jackson
  • 9
  • 1
  • 13