在我的工作中,我们有一个处理JSON文件的服务。
通常,它将较大的文件分割为较小的文件,以便可以多线程并更快地处理更多分割。 有时,我们会看到“ API通信失败”消息,并且在查看日志时,我会看到以下消息:“响应状态代码不表示成功:500(内部服务器错误)”
这并非一直发生,而是每小时发生几次。我们的流量非常大,因此即使出现故障的文件所占的比例也很小。有人可以说明可能出什么问题吗?
这是发生的代码:
public class CommunicationAPI
{
static int iAPIConnectionTimeOut = int.Parse(ConfigurationManager.AppSettings["APIConnectionTimeOut"]); //15 minutes, so a timeout isn't the issue.
public static async Task<string> PostInboundActivityAsync(string sUrl, string sContent, long lSysServiceThread)
{
string sResponse = string.Empty;
try
{
using (HttpClient client = new HttpClient())
{
client.Timeout = TimeSpan.FromSeconds(iAPIConnectionTimeOut);
StringContent strContent = new StringContent("'" + sContent + "'", Encoding.UTF8, "application/json");
var response = client.PostAsync(sUrl, strContent);
var apiResponse = await response;
if (apiResponse != null && HttpStatusCode.PreconditionFailed == apiResponse.StatusCode)
{
return sResponse = apiResponse.Content.ReadAsStringAsync().Result;
}
else
{
apiResponse.EnsureSuccessStatusCode();
}
sResponse = apiResponse.Content.ReadAsStringAsync().Result;
// check if we received response 'OK' 200 status
if (string.IsNullOrWhiteSpace(sResponse))
{
sResponse = apiResponse.ReasonPhrase;
}
}
}
catch (Exception ex)
{
LogManager.WriteToThreadLog("Exception in API Call :" + ex.Message, lSysServiceThread);
sResponse = "EXCEPTION";
}
return sResponse;
}
}
谢谢!