Skymett
Skymett

Reputation: 85

EventHubTrigger C# with EventData object

I am using 1x functions, my question is, if with this version, I can receive an object of type EventData.

I have read documentation about it, but it has not been clear to me.

When executing the function, throw the following exception:

mscorlib: Exception while executing function: eventHubTest. Microsoft.Azure.WebJobs.Host: Se han producido uno o varios errores. Exception binding parameter 'myEventHubMessage'. Microsoft.Azure.WebJobs.Host: Binding parameters to complex objects (such as 'EventData') uses Json.NET serialization. 1. Bind the parameter type as 'string' instead of 'EventData' to get the raw values and avoid JSON deserialization, or 2. Change the queue payload to be valid json. The JSON parser failed: Unable to find a constructor to use for type Microsoft.Azure.EventHubs.EventData. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Origen', line 1, position 10.

This is the header of the function:

using Microsoft.Azure.EventHubs;

[FunctionName("FunctionTest")]
     public static void Run(
         [EventHubTrigger("eventHubTest", Connection = "EventHubConnectionString", ConsumerGroup = "%EventHubConsumerGroup%")]
         EventData[] myEventHubMessage,
         ILogger log)

Upvotes: 0

Views: 4623

Answers (1)

George Chen
George Chen

Reputation: 14324

Suppose I reproduce your problem. In my test if If I use the EventData class in Microsoft.Azure.EventHubs package, it will cause this problem. Then after test I find it should be Microsoft.ServiceBus.Messaging.EventData, so my solution is change the EventData reference to Microsoft.ServiceBus.Messaging, then it will solve this problem.

The below is my work code, you could refer to it.

using Microsoft.Azure.WebJobs;
using System.Text;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Azure.WebJobs.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using Microsoft.Azure.WebJobs.Host;

namespace FunctionApp14
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task RunAsync([EventHubTrigger("geoevent", Connection = "eventcon",ConsumerGroup = "geogroup")]EventData[] events, TraceWriter log)
        {

            var exceptions = new List<Exception>();

            foreach (EventData eventData in events)
            {
                try
                {
                    string messageBody = Encoding.UTF8.GetString(eventData.GetBytes());

                    // Replace these two lines with your processing logic.
                    log.Info($"C# Event Hub trigger function processed a message: {messageBody}");
                    await Task.Yield();
                }
                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);
                }
            }

            // Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure.

            if (exceptions.Count > 1)
                throw new AggregateException(exceptions);

            if (exceptions.Count == 1)
                throw exceptions.Single();
        }
    }
}

Upvotes: 3

Related Questions