Skip to content

Instantly share code, notes, and snippets.

@felipecruz91
Created July 2, 2019 14:24
Show Gist options
  • Save felipecruz91/d5f2df19ed7d7947110142c9fa523b81 to your computer and use it in GitHub Desktop.
Save felipecruz91/d5f2df19ed7d7947110142c9fa523b81 to your computer and use it in GitHub Desktop.
EventHubTriggerFunction - Get PartitionId
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;
using Microsoft.Azure.EventHubs.Processor;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace FunctionApp1
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task Run([EventHubTrigger("your_eventhub", ConsumerGroup = "$Default",
Connection = "EventHubConnectionString")]
EventData[] events, ILogger log, PartitionContext partitionContext)
{
var partitionId = partitionContext.PartitionId;
var exceptions = new List<Exception>();
foreach (var eventData in events)
{
try
{
// Replace these two lines with your processing logic.
log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");
await Task.Yield();
}
catch (Exception e)
{
// We need to keep processing the rest of the batch - capture this exception and continue.
// Also, consider capturing details of the message that failed processing so it can be processed again later.
exceptions.Add(e);
}
}
// Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure.
if (exceptions.Count > 1)
{
throw new AggregateException(exceptions);
}
if (exceptions.Count == 1)
{
throw exceptions.Single();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment