EventHub Azure Function 正确触发但 EventData.Body 为 0 字节
EventHub Azure Function triggers correctly but EventData.Body is 0 bytes
我正在编写一个 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.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);
}
}
}
我怀疑使用两个不同版本的 EventHub 库的原因 - 发送方使用版本 5,Az 函数接收方使用版本 4。
只要考虑到当接收方收到Microsoft.Azure.EventHubs.EventData[=25=时发送方发送Azure.Messaging.EventHubs.EventData ].
详见Guide for migrating to Azure.Messaging.EventHubs from Microsoft.Azure.EventHubs.
尝试在发件人端切换到版本 4,或降级函数以使用 Microsoft.Azure.EventHubs (code ref):
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();
}
我正在编写一个 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.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);
}
}
}
我怀疑使用两个不同版本的 EventHub 库的原因 - 发送方使用版本 5,Az 函数接收方使用版本 4。
只要考虑到当接收方收到Microsoft.Azure.EventHubs.EventData[=25=时发送方发送Azure.Messaging.EventHubs.EventData ].
详见Guide for migrating to Azure.Messaging.EventHubs from Microsoft.Azure.EventHubs.
尝试在发件人端切换到版本 4,或降级函数以使用 Microsoft.Azure.EventHubs (code ref):
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();
}