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:
- https://github.com/Azure/azure-functions-dotnet-worker/pull/508/commits/590e69b9f9fad14730daf0226fce2b93c9acb289
- https://github.com/Azure/azure-functions-dotnet-worker/wiki/.NET-Worker-bindings#using-method-attributes-works-if-you-only-have-one-output-binding
- https://github.com/Azure/azure-functions-dotnet-worker/issues/283
- https://dev.to/kenakamu/azure-function-and-net-5-how-to-get-eventdata-for-event-hub-input-binding-3bmm
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
For those reading in future, here's how the final, working solution looked, with massive help from David Browne in the comments:
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.
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:
Adding a function argument also worked: