微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

EventHub Azure 函数正确触发,但 EventData.Body 为 0 字节

如何解决EventHub Azure 函数正确触发,但 EventData.Body 为 0 字节

我正在编写一个 EventHub 发布者(控制台应用程序)和一个 C# Azure 函数,它将作为消费者工作。当我运行客户端时,我可以看到函数被触发,但它在 eventData.Body 中收到了 0 个字节。有人能帮我一下吗?我在 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);
            }
        }

    }

解决方法

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

只需考虑当接收方收到 Microsoft.Azure.EventHubs.EventData 时,发送方发送 Azure.Messaging.EventHubs.EventData

查看详情Guide for migrating to Azure.Messaging.EventHubs from Microsoft.Azure.EventHubs

尝试在发送方切换到版本 4,或将 Function 降级以使用 Microsoft.Azure.EventHubs (code ref):

File "packet_sniffer.py",line 32,in <module>
    sniff("wlan0")
  File "packet_sniffer.py",line 7,in sniff
    scapy.sniff(iface=interface,store=False,prn=process_sniffed_packets)
  File "/usr/local/lib/python3.8/dist-packages/scapy-2.4.4.dev204-py3.8.egg/scapy/sendrecv.py",line 1058,in sniff
    sniffer._run(*args,**kwargs)
  File "/usr/local/lib/python3.8/dist-packages/scapy-2.4.4.dev204-py3.8.egg/scapy/sendrecv.py",line 1011,in _run
    session.on_packet_received(p)
  File "/usr/local/lib/python3.8/dist-packages/scapy-2.4.4.dev204-py3.8.egg/scapy/sessions.py",line 108,in on_packet_received
    result = self.prn(pkt)
  File "packet_sniffer.py",line 24,in process_sniffed_packets
    print("[+] HTTP Request >>" + url)
TypeError: can only concatenate str (not "bytes") to str

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。