I am writing an EventHub publisher (Console app) and an C# Azure Function that will work as a consumer. when I run the client, I can see the Function getting triggered, but it receives 0 bytes ineventData.Body
. Can anyone help me here? I have seen some other similar questions about empty events received in Az Function. this is differnt, because trigger is fired everytime I send a batch of 10, but somehow the data is eaten up
My function code is
[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();
}
the publisher is also simple and during debugging I can see that the EventData.Body
is 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);
}
}
}
I suspect the reason for using two different versions of EventHub library - version 5 used by the sender, version 4 by your Az function receiver.
Just take into account that sender sends Azure.Messaging.EventHubs.EventData when the receiver gets Microsoft.Azure.EventHubs.EventData.
See for details Guide for migrating to Azure.Messaging.EventHubs from Microsoft.Azure.EventHubs.
Try to switch to version 4 on the sender side, or downgrade the Function to use 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();
}