I am upgrading a .Net 3.1 with Functions v3 to .Net 6 with Functions v4, and I have this EventHubTrigger:
[EventHubTrigger("clean-station-kfi-v1", Connection = "EventHubCleanConnectionString", ConsumerGroup = "local-testing")] EventData[] eventHubMessages
After the upgrade I get this error:
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: KfiPersister
---> System.InvalidOperationException: Exception binding parameter 'eventHubMessages'
---> System.InvalidOperationException: Binding parameters to complex objects (such as 'EventData') uses Json.NET serialization.
1. Bind the parameter type as 'string' instead of 'EventData' to get the raw values and avoid JSON deserialization, or
2. Change the queue payload to be valid json. The JSON parser failed: Unable to find a constructor to use for type Microsoft.Azure.EventHubs.EventData. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Id', line 1, position 6.
I am using Microsoft.Azure.WebJobs.Extensions.EventHubs version 5.0.1
I have this using:
using Microsoft.Azure.EventHubs;
I did find something about using a different namespace like this:
using Azure.Messaging.EventHubs;
Is this the right path or how do I fix this problem here? Switching to that namespace causes lots of other issues though :)
Thank you
Søren
CodePudding user response:
Starting with v5.0, the Microsoft.Azure.WebJobs.Extensions.EventHubs
package moved to the Azure.Messaging.EventHubs
package internally. This means that the types exposed in the Function signature originate in that package.
Since your application includes a using statement for Microsoft.Azure.EventHubs
, the type in your signature is interpreted as:
[EventHubTrigger("clean-station-kfi-v1",
Connection = "EventHubCleanConnectionString",
ConsumerGroup = "local-testing")]
Microsoft.Azure.EventHubs.EventData[] eventHubMessages)
Because the EventData
type belongs to the legacy package, the trigger cannot bind to it.
If you're able, we recommend updating your Function to use Azure.Messaging.EventHubs
, as the Microsoft.Azure.EventHubs
library has been officially deprecated. (ref)
If you have an existing investment in Microsoft.Azure.EventHubs
that makes migrating packages difficult, the most straightforward option would be to use v4.3.1 of the Microsoft.Azure.WebJobs.Extensions.EventHubs
pacakge, which uses the legacy library internally.
Alternatively, you could consider using both Azure.Messaging.EventHubs
and Microsoft.Auzre.EventHubs
in your application and using the full namespace in your binding to disambiguate EventData
; the challenge for that scenario is if you're directly using the Event Hubs clients in your Function body, you would need to copy data between the legacy and current EventData
types.
The binding update would look something like:
[EventHubTrigger("clean-station-kfi-v1",
Connection = "EventHubCleanConnectionString",
ConsumerGroup = "local-testing")]
Azure.Messaging.EventHubs.EventData[] events)