【发布时间】:2020-07-03 20:14:48
【问题描述】:
我有一个在生产环境中运行良好的 Web 服务。 但有时(随机)会引发异常:
à System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)\r\n à System.Threading.Tasks.Task
1.GetResultCore(Boolean waitCompletionNotification)\r\n à System.Threading.Tasks.Task1.get_Result()\r\n à fctSendRequestSynchrone[T](String sRequest, enumHttpMethod eMethod, 对象 oParams)\r\n à API.csRestApi.d__0`1.MoveNext()"
这是我的代码:
.........
//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;
}
你有什么想法吗?
非常感谢您。
埃里克
【问题讨论】:
-
请提供完整的异常,stacktrace只有一部分
-
您可能陷入死锁,因为您在
awaits 异步调用的调用上使用了.Result。
标签: c# rest web-services