skip to Main Content

I have a working Azure Functions app, with an Event Hub trigger.
It’s using Microsoft.Azure.Functions.Worker 1.10, functions runtime 4, .Net 6 and running as Isolated (not in-process).

The current function signature looks like:

    Function("MyFunction")]
        public async Task Run([EventHubTrigger("my-eventhub",
            ConsumerGroup = "a-consumer-group",
            Connection = "EventHub.ConnectionString")] string[] messages)
        {
         //Do stuff
        }

I would like to expand this to grab the Enqueue Time (the time at which the event ended up at the Event Hub).

So, i realise in Functions runtime v4, that i can’t grab EventData[] like i could in older verions, instead i should use the BindingContext.

So i tried this:

    Function("MyFunction")]
        public async Task Run([EventHubTrigger("my-eventhub",
            ConsumerGroup = "a-consumer-group",
            Connection = "EventHub.ConnectionString")] string[] messages, FunctionContext context)
        {
            var data = context.BindingContext.BindingData["enqueuedTimeUtcArray"];
        
            //data would always be empty
        }

Figuring that this was the wrong approach, and seeing further examples breaking fields down, i tried the following:

    Function("MyFunction")]
        public async Task Run([EventHubTrigger("my-eventhub",
            ConsumerGroup = "a-consumer-group",
            Connection = "EventHub.ConnectionString")] string[] messages,
            DateTime[] enqueuedTimeUtcArray,
            long[] sequenceNumberArray,
            string[] offsetArray,
            Dictionary<string, JsonElement>[] propertiesArray,
            Dictionary<string, JsonElement>[] systemPropertiesArray))
        {
            var data = enqueuedTimeUtcArray;
        
            //data would always be empty
    }

I’ve tried various variations on those themes, including referencing the following:

The functions still trigger, and the functionality it provides (replaced with //do stuff) still goes off without a hitch, but still no EnqueueTimes… what am i doing wrong?

2

Answers


  1. Chosen as BEST ANSWER

    For those reading in future, here's how the final, working solution looked, with massive help from David Browne in the comments:

            Function("MyFunction")]
            public async Task Run([EventHubTrigger("my-eventhub",
                ConsumerGroup = "a-consumer-group",
                Connection = "EventHub.ConnectionString")] string[] messages, FunctionContext context)
            {
                var data = context.BindingContext.BindingData["enqueuedTimeUtcArray"];
            
                var listOfDateTimes = JsonSerializer.Deserialize<string[]>(data["EnqueuedTimeUtcArray"].ToString())
                            .Select(a => DateTime.Parse(a.ToString())).ToList()
    
              //Do stuff
            }
    

    I finally started receiving data from the FunctionContext after restarting my PC (I have no idea...), then realized i'd received a json array of strings, which were datetime compatible. So, deserializing them was the answer.


  2. For isolated functions you have to dig around in the FunctionContext to get this data. The message enqueue times are available as a JSON array:

        [Function("EventHubTrigger1")]
        public void Run([EventHubTrigger("test", Connection = "myhub_all_EVENTHUB")] string[] input, FunctionContext context)
        {
    
            string enquedTimes = context.BindingContext.BindingData["EnqueuedTimeUtcArray"];
    
            _logger.LogInformation($"First Event Hubs triggered message: {input[0]}");
        }
    

    Adding a function argument also worked:

    public void Run([EventHubTrigger("test", Connection = "myhub_all_EVENTHUB")] string[] input, string[] enqueuedTimeUtcArray)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search