我正在编写一个 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.Body是System.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);
}
}
}