Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
419 stars 184 forks source link

Azure Function with Event Hub trigger and `EventData` as argument, fails with serialization error #2481

Open sunefred opened 4 months ago

sunefred commented 4 months ago

Description

I am using the sample from https://github.com/Azure/azure-functions-dotnet-worker/blob/main/samples/Extensions/EventHubs/EventHubsFunction.cs, in order to isolate my issue. When running the functions that takes EventData as argument I get a serialization exception from Azure.Core.Amqp. In particular this function from the sample should work:

[Function(nameof(EventDataFunctions))]
public void EventDataFunctions([EventHubTrigger("queue", Connection = "EventHubConnection", IsBatched = false)] EventData @event)
{
    _logger.LogInformation("Event Body: {body}", @event.Body);
    _logger.LogInformation("Event Content-Type: {contentType}", @event.ContentType);
}

My .csproj, Program.cs, host.json and local.settings.json are all copied from the sample above, and only modified to point to my EventHub in Azure. That is, the sample uses Azure Function version 4, and isolated worker model.

The following exception is thrown

System.Private.CoreLib: Exception while executing function: Functions.EventDataFunctions. Microsoft.Azure.WebJobs.Host:
Exception binding parameter 'event'. Azure.Core.Amqp: Serialization failed due to an unsupported type, System.Byte[].

Steps to reproduce

  1. Create a project from the sample listed above.
  2. Configure local.settings.json
  3. Run
hw13349 commented 4 months ago

Same here after migrating from .net6 (in-process) to .net8 isolated worker model for events having byte array/base64 properties.

Workaround is to bind to raw event data (byte[]):

[EventHubTrigger("xx", Connection = "Connection", ConsumerGroup = "yy", IsBatched = false)] byte[] eventData,
            IDictionary<String, Object>  properties, IDictionary<String, Object> systemProperties)

But accessing properties in batched mode seems not possible anymore.