1
votes

I have an Azure Function triggered by Event Hub. I configured the function app in the host.json with a MaxBatchSize of 32. My Event Hub has 6 partitions, and I don't want an event to be processed twice.

So, I need partition ID and sequence number to identify the event uniquely. I would like to save PartitionID and SequenceNumber in my database as primary keys. My function is triggered in input with Array of EventData. Iterating on the array, I can get the SequenceNumber for each message, but I don't know how to get per PartitionID. I tried to include parameter of type PartitionContext among input parameters but it doesn't work.

Here it is my code:

[FunctionName("EventHubTriggeredFunction")]
public static void Run([EventHubTrigger("events", Connection = "EventHubConnection")]EventData[] eventHubMessages, TraceWriter log)
{
    foreach (var message in eventHubMessages)
    {
        using (Stream stream = message.GetBodyStream())
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                try
                {
                    //... do something

                    //SequenceNumber: message.SequenceNumber
                    Save(reader.ReadToEnd(), message);

                    //... do something else
                }
            }
        }
 }

How can I get PartitionID for each message processed?

1
What makes you think your message gets processed twice? Normal behavior is for message to be processed once.Peter Bons
@PeterBons Got it. I thought a message could be processed multiple times until his expiration (1 day in my case). I'm a beginner with event hub. Thank you for clarification.Alessio Innocenzi
That one day (we are talking about the retention, are we?) is the minimum days the message is kept. In real world I have seen them sitting there for weeks, even monthsPeter Bons
You can process older messages but it requires some (manual) steps but it is not default behavior.Peter Bons

1 Answers

3
votes

Define the EventHub trigger to include Event Metadata. Then use the receiverRuntimeInfo on the context object.

PartitionContext.RuntimeInfo.PartitionId