Search code examples
c#azureazure-functionsazure-eventhub

EventHubTrigger C# with eventData object does not work


I have created an EventHubTrigger, which looks like this:

using System;

public static void Run(string myEventHubMessage, ILogger log)
{
log.LogInformation($'C# Event Hub trigger function processed a message: {myEventHubMessage}');
}

This did work without any problems, but since I do need additional meta information, I changed the code to the following (described in the second linked article):

#r 'Microsoft.ServiceBus'
using System.Text;
using System;
using Microsoft.ServiceBus.Messaging;

public static void Run(EventData myEventHubMessage, ILogger log)
{
log.LogInformation($'EnqueuedTimeUtc={myEventHubMessage.EnqueuedTimeUtc}');
log.LogInformation($'SequenceNumber={myEventHubMessage.SequenceNumber}');
log.LogInformation($'Offset={myEventHubMessage.Offset}');
} 

But this code results in the following error messages (btw I have also tied to use the deprected TraceWriter instead of ILogger to exactly follow the article but this results in the same error)

2018-10-11T14:22:24.814 [Error] run.csx(1,1): error CS0006: Metadata file 'Microsoft.ServiceBus' could not be found 
2018-10-11T14:22:24.903 [Error] run.csx(4,17): error CS0234: The type or namespace name 'ServiceBus' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) 

My question is now, does anyone have a clue what to do in order to get this small piece of code running?


Solution

  • Well, the sample is for function 1.x. After 2.x is generally available the function we create is on ~2 runtime by default, as we can see "version":"2.0" in host.json.

    Have a try at code below, metadata is stored in SystemProperties of Microsoft.Azure.EventHubs.EventData.

    #r "../bin/Microsoft.Azure.EventHubs.dll"
    
    using System;
    using Microsoft.Azure.EventHubs;
    
    public static void Run(EventData myEventHubMessage, ILogger log)
    {
        log.LogInformation($"EnqueuedTimeUtc={myEventHubMessage.SystemProperties.EnqueuedTimeUtc}");
        log.LogInformation($"SequenceNumber={myEventHubMessage.SystemProperties.SequenceNumber}");
        log.LogInformation($"Offset={myEventHubMessage.SystemProperties.Offset}");
    }
    

    Also note that we need to use double quotation " for string in C#, see ' in your code.