【问题标题】:Random exception on Web service callWeb 服务调用的随机异常
【发布时间】:2020-07-03 20:14:48
【问题描述】:

我有一个在生产环境中运行良好的 Web 服务。 但有时(随机)会引发异常:

à System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)\r\n à System.Threading.Tasks.Task1.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


【解决方案1】:

Roman Kalinchuk,Crowcoder:感谢您的回复。 罗曼·卡林丘克: 这是整个堆栈跟踪:

à 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   à logs.csLogWS.fctSendRequestSynchrone[T](String sRequest, enumHttpMethod eMethod, Object oParams)\r\n   à logs.csLogWS.sbrErrorLigneRejet(String sMessage, String sCdRejet, Exception oException)\r\n   à logs.csLogWS.ligneLogInformation(String sMessage)\r\n   à Importation.Program.sbrImportUnitaire(String sCdInfImp, String sFileNameIn, String sFileStructure, String sLiProcedure, String[] args)|   à Service.API.csRestApi.<SendRequest>d__0`1.MoveNext()".

我在之前的问题帖子中对其进行了简化。 该应用程序在生产环境中,我是否必须将 pdb 文件放在生产服务器上才能获得更多堆栈跟踪?

Crowcoder:你的意思是 .Result 正在等待,而 SendRequest 也在等待?你有什么建议?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多