Search code examples
service.net-coredotnet-httpclient

.Net Core Service HTTPClient Call Inconsistently Returning Bad Request


I have a .net core service running as an application service. The following call runs every 5 seconds for a few hours and then the service starts getting 400 errors. If I start and stop the service the application begins to function appropriately.

 _httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true };
    _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    string utcDateOut = DateTime.UtcNow.ToString("s", CultureInfo.InvariantCulture);
    var sURI = _arlnPortalBaseUrl + _arlnPortalSvcDownloads + "?$filter=downloadRequestStatus ne 'Completed' and downloadRequestStatus ne 'Failed'&timeStamp=" + utcDateOut ;
    var downloadResponse = await _httpClient.GetAsync(new Uri(sURI));

The logs show the following information about the HTTP Client Request.

2019-12-20 12:12:48.2109|100|INFO|System.Net.Http.HttpClient.Default.ClientHandler|Sending HTTP request GET http://10.32.1.132/arln-portal-service/odata/Downloads?$filter=downloadRequestStatus ne 'Completed' and downloadRequestStatus ne 'Failed'&timeStamp=2019-12-20T17:12:48 |url: |action: 
2019-12-20 12:12:48.2282|101|INFO|System.Net.Http.HttpClient.Default.ClientHandler|Received HTTP response after 10.6963ms - BadRequest |url: |action: 
2019-12-20 12:12:48.2282|101|INFO|System.Net.Http.HttpClient.Default.LogicalHandler|End processing HTTP request after 19.1219ms - BadRequest |url: |action: 
2019-12-20 12:12:48.2282||ERROR|arln_sqs_service.Worker|Issue connecting to the ArlnPortalService at: http://10.32.1.132/arln-portal-service/odata/Downloads?$filter=downloadRequestStatus ne 'Completed' and downloadRequestStatus ne 'Failed'&timeStamp=2019-12-20T17:12:48. StatusCode is not 200. Unable to query the pending downloads. downloadResponse Status Code: BadRequest |url: |action: 
2

Solution

  • The issue was caused because I was endlessly adding default request headers in a loop. I changed the code to clear the request headers before adding defaults and the service runs fine now.