EventHubTrigger C# 与 EventData 对象

EventHubTrigger C# with EventData object

我正在使用 1x 函数,我的问题是,如果使用此版本,我可以接收 EventData 类型的对象。

我已经阅读了有关它的文档,但我还不清楚。

执行函数时,抛出如下异常:

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.

这是函数的头文件:

using Microsoft.Azure.EventHubs;

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

假设我重现了你的问题。在我的测试中如果我使用 EventData class in Microsoft.Azure.EventHubs 包,它会导致这个问题。然后测试后发现应该是Microsoft.ServiceBus.Messaging.EventData,所以我的解决办法是把EventData的引用改成Microsoft.ServiceBus.Messaging,就解决了这个问题。

下面是我的工作代码,你可以参考一下

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();
        }
    }
}