0
votes

Literally trying out to make do of something I am not good at.

I have read upon the durable function overview here - https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-overview.

There is a topic on using Bindings to use it on an Event Hub Trigger, but there is not really a working example, I followed what was there and came up with this binding in my function.json,

{
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "myEventHubMessage",
      "direction": "in",
      "path": "testinhub",
      "connection": "Endpoint=sb://dev-testingeventhubinns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=lLassdff5Y/esH8/CaXDOWH0jF2JtZBQhQeFoCtfqYs=",
      "consumerGroup": "$Default"
    },
    {
      "type": "eventHub",
      "name": "outputEventHubMessage",
      "connection": "Endpoint=sb://dev-testingeventhuboutns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=4yuasdff7Lzu+mQJFVlnlozUItqFY1L3WW/kJnpTjq8=",
      "path": "testouthub",
      "direction": "out"
    }
  ],
  "disabled": false,
  "entryPoint": "EventTriggerFunction.EventHubTriggerClass.Run"
}

My code in entirety is as follows,

using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.EventHubs;
using System;

namespace EventTriggerFunction
{
    public static class EventHubTriggerClass
    {
        [FunctionName("EventHubTrigger")]
        public static async Task<List<string>> Run([OrchestrationTrigger] DurableOrchestrationContext context)
        {
            await context.CallActivityAsync<string>("EventHubTrigger_Send", "Hello World");

            return null;
        }

        [FunctionName("EventHubTrigger_Send")]
        public static void SendMessages([EventHubTrigger("testinhub", Connection = "ConnectionValue")] EventData[] eventHubMessages, ILogger log)
        {
            var exceptions = new List<Exception>();

            foreach (EventData message in eventHubMessages)
            {
                try
                {
                    log.LogInformation($"C# Event Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body)}");
                }
                catch (Exception e)
                {
                    // We need to keep processing the rest of the batch - capture this exception and continue.
                    // Also, consider capturing details of the message that failed processing so it can be processed again later.
                    exceptions.Add(e);
                }
            }            
        }
    }
}

If I send a message using the function, I can not see it on my testouthub event hub. Not really sure how this Durable function and EventHub Trigger works hand in hand.

1

1 Answers

3
votes

I think you are mixing it up a bit. When using the attributes like FunctionName and EventHubTrigger you do not need to supply the function.json. It's either function.json or with attributes.

If you are trying to receive messages from 1 eventhub and passing it on to the next, then something like this below will also do the trick. There's no need to use DurableFunctions for this and the Azure Function runtime will scale by itself if there are many messages, see this

[FunctionName("EventHubTriggerCSharp")]
[return: EventHub("outputEventHubMessage", Connection = "EventHubConnectionAppSetting")]
public static void Run([EventHubTrigger("samples-workitems", Connection = "EventHubConnectionAppSetting")] string myEventHubMessage, ILogger log)
{
    log.LogInformation($"C# Event Hub trigger function processed a message: {myEventHubMessage}");

    return myEventHubMessage;
}

An additional tip: I would not paste your full connectionstring into StackOverflow. Maybe it's wise to immediately create new AccessKeys for your eventhubs.