Random exception on Web service call RRS feed

  • Question

  • Hello everybody,

    I have a Web service which works fine in production environment.
    But sometimes (randomly) an exception is raised :

    à System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)\r\n

    à System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)\r\n

    à System.Threading.Tasks.Task`1.get_Result()\r\n

    à fctSendRequestSynchrone[T](String sRequest, enumHttpMethod eMethod, Object oParams)\r\n

    à API.csRestApi.<SendRequest>d__0`1.MoveNext()"

    Here is my code :

    .........//Here is the line which raises the exception :

    fctSendRequestSynchrone<string>(string.Format("logs/{0}/message", _lIdLog), cs.enumHttpMethod.POST, oLogLigne);



    private T fctSendRequestSynchrone<T>(string sRequest, csRestApi.enumHttpMethod eMethod, object oParams = null)


    Task<T> otask = fctSendRequest<T>(sRequest, eMethod, oParams);

    return otask.Result;



    public async Task<T> SendRequest<T>(string sAction, enumHttpMethod eMethod, object oParams = null)


    string sResponse = string.Empty;

    T oValue;

    using (var oClient = new HttpClient(new LogginHandler(_oCnx, new HttpClientHandler())))



    oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    string sRequest = string.Concat(_sUrlApi, "/", sAction);

    if (_oToken != null)


    oClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(_oToken["token_type"], _oToken["access_token"]);


    using (HttpResponseMessage oResponse = await oClient.PostAsJsonAsync(sRequest, oParams))


    if (oResponse.IsSuccessStatusCode)


    HttpContent content = oResponse.Content;

    sResponse = await content.ReadAsStringAsync();




    throw new RestApiException(oResponse);




    oValue = JsonConvert.DeserializeObject<T>(sResponse);

    return oValue;


    Do you have an idea ?

    Thank you very much in advance.


    • Edited by eric.bryan Friday, July 3, 2020 8:27 PM
    Friday, July 3, 2020 8:20 PM

All replies

  • Which exception is thrown? (See the details before the ‘à’ lines).

    If Visual Studio shows an exception object during debugging, then check its Message and InnerException properties.

    Saturday, July 4, 2020 8:36 AM
  • What does the line that throws the exception have to do with all that HttpClient code you posted after that? The request that is failing is being done in `fctSendRequest` which you didn't provide. That returns a task which blows up when you try to get the result. Nothing in the HttpClient code you posted after that matches that line.

    I should also point out that your HttpClient code is wrong and will cause you to eventually get errors sending requests because you are using up all your connections. Refer to the documentation on HttpClient on why you should never do this and how to fix it. Also refer to the documentation on how to properly associate headers on a per-request basis otherwise multiple threads will collide in the middle as HttpClient's properties are not threadsafe.

    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, July 4, 2020 2:38 PM