Skip to content

Error with v4 EventHub trigger and cardinality many #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rvk86 opened this issue Nov 3, 2023 · 5 comments
Closed

Error with v4 EventHub trigger and cardinality many #187

rvk86 opened this issue Nov 3, 2023 · 5 comments

Comments

@rvk86
Copy link

rvk86 commented Nov 3, 2023

I'm upgrading my EventHub trigger to the v4 programming model. When I run the trigger locally and publishing a message the code works fine with cardinality: "one", but when I switch to cardinality: "many" I'm receiving the following error:

[2023-11-03T09:33:04.431Z] Executed 'Functions.location-updates' (Failed, Id=eac578f2-f16e-470b-8474-601a02d0e0b8, Duration=59ms)
[2023-11-03T09:33:04.432Z] System.Private.CoreLib: Exception while executing function: Functions.location-updates. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'eventHubTrigger1'. Microsoft.Azure.WebJobs.Host: Binding parameters to complex objects (such as 'Object') uses Json.NET serialization. 
[2023-11-03T09:33:04.432Z] 1. Bind the parameter type as 'string' instead of 'Object' to get the raw values and avoid JSON deserialization, or
[2023-11-03T09:33:04.432Z] 2. Change the queue payload to be valid json. The JSON parser failed: Unexpected character encountered while parsing value: . Path '', line 0, position 0.

Repro steps

  1. Run the below code locally with func start
  2. Publish a message to the EventHub the trigger is listening to
  3. Observe the error being thrown
import { app } from "@azure/functions";

const eventHubTrigger = async function (
    eventHubMessages: any
): Promise<void> {
    console.log(eventHubMessages);
};

app.eventHub("location-updates", {
    eventHubName: process.env.EVENT_HUB_NAME,
    connection: "EVENT_HUB_CONNECTION",
    handler: eventHubTrigger,
    cardinality: "many", // if this is set to "one" the trigger works fine
});

Expected behavior

I'd expect the handler function to run with an array of strings as the first argument.

Actual behavior

The emulator throws the error as shown above

@ejizba
Copy link
Contributor

ejizba commented Nov 6, 2023

Hi @rvk86 this is somewhat of a known problem, tracked by Azure/azure-functions-eventhubs-extension#118

The recommended workarounds are to either make sure your event hub messages are JSON-parseable or to add dataType: "string" to your configuration.

@rvk86
Copy link
Author

rvk86 commented Nov 6, 2023

Hi @ejizba, thanks for your response. Since my messages are in Avro format, your first workaround is not feasible. I'd love to try the second, but with the v4 programming model I wouldn't know where to define the dataType. Can you help me with that?

@ejizba
Copy link
Contributor

ejizba commented Nov 6, 2023

Try this:

app.eventHub("location-updates", {
    eventHubName: process.env.EVENT_HUB_NAME,
    connection: "EVENT_HUB_CONNECTION",
    handler: eventHubTrigger,
    cardinality: "many", // if this is set to "one" the trigger works fine
    dataType: "string",
});

We don't have full support for "dataType" yet in the TypeScript types (tracked here), but functionality-wise it should work. If TypeScript complains, casting the options object to "any" is an easy way to suppress the error:

app.eventHub("location-updates", <any>{

@rvk86
Copy link
Author

rvk86 commented Nov 7, 2023

specifying the dataType in the eventHubOptions indeed solves the problem! Would be nice to update the type definitions to avoid this confusion.

Thanks for your help :)

@ejizba
Copy link
Contributor

ejizba commented Nov 7, 2023

Nice! Closing in favor of #72 and Azure/azure-functions-eventhubs-extension#118

@ejizba ejizba closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants