Eric Bryan

Eric Bryan

  • NA
  • 18
  • 7.2k

Random exception on Web service call

Jul 3 2020 3:02 PM
Hello everybody,
I have a Web service which works fine in production environment.
But sometimes (randomly) an exception is raised :
  1. à System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)\r\n  
  2. à System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)\r\n  
  3. à System.Threading.Tasks.Task`1.get_Result()\r\n  
  4. à fctSendRequestSynchrone[T](String sRequest, enumHttpMethod eMethod, Object oParams)\r\n  
  5. à API.csRestApi.<SendRequest>d__0`1.MoveNext()"  
Here is my code :
  1. .........  
  2.   
  3. //Here is the line which raises the exception :  
  4. fctSendRequestSynchrone<string>(string.Format("logs/{0}/message", _lIdLog), cs.enumHttpMethod.POST, oLogLigne);  
  5.   
  6. .........  
  7. //-------------------------------------------------------------------------------------  
  8.   
  9. private T fctSendRequestSynchrone<T>(string sRequest, csRestApi.enumHttpMethod eMethod, object oParams = null)  
  10. {  
  11. Task<T> otask = fctSendRequest<T>(sRequest, eMethod, oParams);  
  12. return otask.Result;  
  13. }  
  14.   
  15. //-------------------------------------------------------------------------------------  
  16.   
  17. public async Task<T> SendRequest<T>(string sAction, enumHttpMethod eMethod, object oParams = null)  
  18. {  
  19.   
  20. string sResponse = string.Empty;  
  21. T oValue;  
  22.   
  23. using (var oClient = new HttpClient(new LogginHandler(_oCnx, new HttpClientHandler())))  
  24. {  
  25. oClient.DefaultRequestHeaders.Accept.Clear();  
  26. oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  27.   
  28. string sRequest = string.Concat(_sUrlApi, "/", sAction);  
  29.   
  30. if (_oToken != null)  
  31. {  
  32. oClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(_oToken["token_type"], _oToken  
  33. ["access_token"]);  
  34. }  
  35.   
  36. using (HttpResponseMessage oResponse = await oClient.PostAsJsonAsync(sRequest, oParams))  
  37. {  
  38. if (oResponse.IsSuccessStatusCode)  
  39. {  
  40. HttpContent content = oResponse.Content;  
  41. sResponse = await content.ReadAsStringAsync();  
  42. }  
  43. else  
  44. {  
  45. throw new RestApiException(oResponse);  
  46. }  
  47. }  
  48.   
  49. }  
  50.   
  51. oValue = JsonConvert.DeserializeObject<T>(sResponse);  
  52.   
  53. return oValue;  
  54. }  
Do you have an idea ?
Thank you very much in advance.
Eric

Answers (1)