DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

Azure Function and .NET 5: How to get EventData for Event Hub input binding

How binding works in .NET 5 version of Azure Function changed slightly compared to last version. In this article, I explain how you can get EventData for Event Hub input binding.

Get Event Hub message content

This is template code and you can see the message comes in as string array.

using System;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace eventhubbinding
{
    public static class eventhubtriggerdemo
    {
        [Function("eventhubtriggerdemo")]
        public static void Run([EventHubTrigger("samples-workitems", Connection = "")] string[] input, FunctionContext context)
        {
            var logger = context.GetLogger("eventhubtriggerdemo");
            logger.LogInformation($"First Event Hubs triggered message: {input[0]}");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

input argument contains message from EventHub.
image

How about EventData?

We can get EventData from FunctionContext. I don't feel this is ideal way to obtain data, but we can get it anyway.

image

There is a GitHub issue for this as well.
https://github.com/Azure/azure-functions-dotnet-worker/issues/283

Summary

I hope we can switch signature from string[] to EventData like we did in previous version, but this is how it works for now.

Top comments (0)