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);
using (var oClient = new HttpClient(new LogginHandler(_oCnx, new HttpClientHandler()))) { oClient.DefaultRequestHeaders.Accept.Clear(); oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Member
3 Points
39 Posts
Random exception on Web service call
Jul 03, 2020 07:49 PM|eric.bryan|LINK
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 = SendRequest<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.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
All-Star
53081 Points
23655 Posts
Re: Random exception on Web service call
Jul 04, 2020 10:17 AM|mgebhard|LINK
I'm not sure why you shared the SendRequest method as it is not used or part of the error message.
The fctSendRequestSynchrone method wastes a thread and blocks. I assume the actual problem is with the fctSendRequest which is not shared.
Member
3 Points
39 Posts
Re: Random exception on Web service call
Jul 04, 2020 02:25 PM|eric.bryan|LINK
Thank you for your reply mgebhard.
In fact I was wrong, I corrected the fctSendRequest into SendRequest.
I have updated my post.