Forum Discussion
Why is element in the queue deleted even if the function throws an exception?
How about try this?
[Function("QueueCancellations")]
public async Task QueueCancellation([QueueTrigger("requests", Connection = "ConnectionStrings:QUEUE_CONNECTION_STRING")] string message)
{
try
{
using (var httpClient = new HttpClient())
{
var content = new StringContent(message, Encoding.UTF8, "application/json");
var httpResponse = await httpClient.PostAsync(_configuration["LOCAL_SERVICE_URL_CANCELL"], content);
if (httpResponse.IsSuccessStatusCode)
{
_logger.LogInformation("Data sent to backend successfully.");
}
else
{
_logger.LogWarning("Backend not available, retrying...");
throw new Exception("Backend not available");
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending data to backend, retrying...");
throw; // This will cause the message to be retried
}
}