0

C# 10/.NET 6

I am struggling to identify what is causing this error. From this code:

internal class Call
{
    internal int RetryCount { get; set; } = 1;

    internal HttpClient Client { get; init; }
    internal HttpRequestMessage Request { get; private set; }

    internal Call(HttpClient client, HttpRequestMessage request)
    {
        ...
    }

    internal async Task<T> SendRequestAsync<T>()
    {
        JsonSerializerOptions serializerOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };

        while (true)
        {
            if (this.RetryCount > Setup.Config.RetryMax)
                throw new TimeoutException($"Maximum number of retries reached.");
            
            await LogRequest(this.Client, this.Request);

            Thread.Sleep(Setup.Config.ThrottleBuffer);

            try
            {
                HttpResponseMessage? response = new();
                response = await this.Client.SendAsync(this.Request);

                await LogResponse(response);
                _ = response.EnsureSuccessStatusCode();

                return await response.Content.ReadFromJsonAsync<T>(serializerOptions) ?? throw new Exception("Result object was null after JSON deserialization.");
            }
            catch (HttpRequestException ex)
            {
                if (ex.StatusCode is HttpStatusCode.TooManyRequests or HttpStatusCode.InsufficientStorage or HttpStatusCode.ServiceUnavailable)
                {
                    Log.Error($"HTTP request failed.  Retrying in {(Setup.Config.RetryDelay / 1000) * this.RetryCount} seconds...");
                    Thread.Sleep(Setup.Config.RetryDelay * this.RetryCount);
                    this.RetryCount++;

                    continue;
                }
                else if (ex.StatusCode is HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden)
                {
                    Log.Error($"HTTP request failed.  Permissions error: unable to retry.");
                    throw;
                }
                else
                {
                    throw;
                }
            }
        }
    }
}

When called repeatedly, it runs 15 times and then errors on the 16th run. I get this error:

Object name: System.Net.Http.HttpClient.
   at System.Net.Http.HttpClient.CheckDisposed()
   at System.Net.Http.HttpClient.CheckRequestBeforeSend(HttpRequestMessage request)
   at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request)
   at [redacted].Call.SendRequestAsync[T]() in C:\Users\[redacted]\Call.cs:line 97

Line 97 points to this line: response = await this.Client.SendAsync(this.Request)

I found this thread where a user had a similar problem with StringContent being automatically disposed by HttpClient. However, I have HttpResponseMessage being created each time in the loop.

What am I missing?

chrisxfire
  • 445
  • 1
  • 3
  • 12

1 Answers1

1

The error was pointing to the HttpClient being disposed, not HttpResponseMessage. The consumer was disposing the client prematurely.

chrisxfire
  • 445
  • 1
  • 3
  • 12