Reputation: 231
I have Function in C# Which posts a certain ledger in a JSON format to a SAP
endpoint. I have 12 such JSON that needs to be posted. But as it happens 11/12 JSON post successfully, but the last one gets timed out
. From my side I have increased the Max timeout limit to 5 minutes
The timed-out response says
A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond. (interfaces.confidential.com:443).
Also the timeout response returns 200 status code even after timing out
.
I also have a retry policy in place if the response is not 200.
public async Task<string> InvokePostAsync(string jsonString,string clientId, string fileName)
{
string result = null;
HttpResponseMessage response = new HttpResponseMessage();
string submissionTime = null;
string responseTime = null;
try
{
if (jsonString.IsNotNullEmptyOrWhitespace()) {
var client = new HttpClient();
client.Timeout = TimeSpan.FromMinutes(5); // max timeout limit set to 5 minutes
var retryPolicy = Policy
.HandleResult<HttpResponseMessage>(r => r.StatusCode == System.Net.HttpStatusCode.InternalServerError)
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(3));
submissionTime = DateTime.Now.ToString(); //retry after 3 sec if not 200 status
response = await retryPolicy.ExecuteAsync(async () =>
{
var request = new HttpRequestMessage(HttpMethod.Post, new Uri( _appSettings.postEndpoint) );
request.Headers.Add("Accept", "application/json");
request.Headers.Add("Authorization", $"Basic {_appSettings.AuthToken}");
request.Content = new StringContent(jsonString);
return await client.SendAsync(request);
});
responseTime = DateTime.Now.ToString();
response.EnsureSuccessStatusCode();
result = await response.Content.ReadAsStringAsync();
_logger.LogInformation($"Post result for client {clientId}:" + result);
}
return await createEmailMessageforClient(true,clientId,submissionTime,responseTime,fileName,result,(int)response.StatusCode);
}
catch (Exception ex)
{
responseTime = DateTime.Now.ToString();
_logger.LogError($"Error while posting web service for client: {clientId} Exception: {ex.Message}");
return await createEmailMessageforClient(false, clientId, submissionTime, responseTime, fileName, ex.Message, (int)response.StatusCode);
}
}
I feel like from my side I have done Everything fine, And I think The SAP endpoint has a rate limit set to it. Please let me know if I can tackle this issue from my code itself. OR is it only configurable using the SAP interface?
Upvotes: 0
Views: 58