DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

5 2

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.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay