Random exception on Web service call

eric.bryan

Member
Joined
Jun 15, 2014
Messages
8
Programming Experience
3-5
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 :
C#:
fctSendRequestSynchrone<string>(string.Format("logs/{0}/message", _lIdLog), cs.enumHttpMethod.POST, oLogLigne);
C#:
private T fctSendRequestSynchrone<T>(string sRequest, csRestApi.enumHttpMethod eMethod, object oParams = null)
{
   Task<T> otask = fctSendRequest<T>(sRequest, eMethod, oParams);
   return otask.Result;
}

C#:
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.Clear();
      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();
         }
         else
         {
            throw new RestApiException(oResponse);
         }
      }

   }

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

   return oValue;
}

Do you have an idea ?

Thank you very much in advance.

Eric
 
First thing I'd do is try setting a higher timeout on your Httpclient. It's just a hunch.

I'd also add some logging for exception handlers. This will tell you a bit more about the error in general.

I've only glanced, but maybe someone else might see something I've overlooked.
 
Probably not directly related to your issue, but worth a read since I saw that you were not using a Singleton of a HttpClient:

 
Probably not directly related to your issue, but worth a read since I saw that you were not using a Singleton of a HttpClient:
This is also clearly written in documentation:
HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors.
 
Back
Top Bottom