1

我正在编写一个 EventHub 发布者(控制台应用程序)和一个可以作为消费者使用的 C# Azure 函数。当我运行客户端时,我可以看到函数被触发,但它在eventData.Body. 有人能帮我一下吗?我已经看到其他一些关于 Az Function 中收到的空事件的类似问题。这是不同的,因为每次我发送一批 10 时都会触发触发器,但不知何故数据被吃掉了

我的功能代码是

    [FunctionName("EventHubTrigger1")]
    public static async Task Run([EventHubTrigger("confighub", Connection = "EventHubName")] EventData[] events, ILogger log)
    {
        var exceptions = new List<Exception>();

        foreach (EventData eventData in events)
        {
            try
            {
                //eventData.Body is System.ReadOnlyMemory<Byte>[0] instead of what the sender is sending
                string messageBody = Encoding.UTF8.GetString(eventData.Body.ToArray());

                
                log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");
                await Task.Yield();
            }
            catch (Exception e)
            {
               
                exceptions.Add(e);
            }
        }

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

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

发布者也很简单,在调试过程中我可以看到EventData.BodySystem.ReadOnlyMemory<Byte>[456]

private async Task SendToHub(IEnumerable<IDomain> users)
    {
        await using (var producerClient = new EventHubProducerClient(_eventHubConnectionString, _eventHubName))
        {
            try
            {
                CreateBatchOptions options = new CreateBatchOptions();
                
                options.PartitionKey = "user";
                using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(options);

                foreach (var user in users)
                {
                    var json = JsonSerializer.Serialize(user);
                    
                    eventBatch.TryAdd(new Azure.Messaging.EventHubs.EventData(Encoding.UTF8.GetBytes(json)));
                }
                //During Debugging I can see that the Body is 456 bytes
                await producerClient.SendAsync(eventBatch);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

    }
4

1 回答 1

1

我怀疑使用两个不同版本的 EventHub 库的原因 - 发送方使用的版本 5,Az 函数接收方使用的版本 4。

只需考虑发送方在接收方获取Microsoft.Azure.EventHubs.EventData时发送Azure.Messaging.EventHubs.EventData即可。

有关从 Microsoft.Azure.EventHubs 迁移到 Azure.Messaging.EventHubs 的详细信息,请参阅指南

尝试在发送方切换到版本 4,或将函数降级以使用 Microsoft.Azure.EventHubs(代码参考):

var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
var eventHubName = "<< NAME OF THE EVENT HUB >>";

var connectionStringBuilder = new EventHubsConnectionStringBuilder(connectionString){ EntityPath = eventHubName }; 
var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

try
{
    EventData eventData = new EventData(Encoding.UTF8.GetBytes("First"));
    await eventHubClient.SendAsync(eventData, "my-partition-key");
}
finally
{
    await eventHubClient.CloseAsync();
}
于 2020-12-28T20:45:33.250 回答