It will be nice to have this feature built in the output bindings. However, the following code snippet shows an example of the retrying message to the Event Hub. The concept is based on the catching an error on the output binding and sending a message to the storage queue for retrying process:
[FunctionName("Function4")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestMessage req,
[EventHub("%EventHub%", Connection = "connectionEventHub")] IAsyncCollector<string> outputEventHubMessages,
[Queue("%RetryQueue%")] IAsyncCollector<string> outputQueueRetryMessages,
TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
// Get request body
string message = JsonConvert.SerializeObject(JObject.Parse(await req.Content.ReadAsStringAsync()));
try
{
await outputEventHubMessages.AddAsync(message);
await outputEventHubMessages.FlushAsync();
}
catch(Exception ex)
{
log.Error(ex.Message);
//await Task.Delay(5000);
await outputQueueRetryMessages.AddAsync(message);
}
return req.CreateResponse(HttpStatusCode.OK);
}
the queue can be configured in the host.json file:
{
"queues": {
"maxDequeueCount": 3,
"visibilityTimeout": "00:00:30"
}
}