2
votes

I have an azure function with EventHubTrigger:

    [FunctionName("TradesDataProcessStarterEh")]
    public static async Task TradesDataProcessStarterEh([EventHubTrigger("aeehrobotronapiintegrationdev", Connection = "EventHubConnectionString", ConsumerGroup = "$Default")]
        EventData eventData, PartitionContext partitionContext, [OrchestrationClient] DurableOrchestrationClient starter, ILogger log)
    {
        if (partitionContext.PartitionId != "1")
            return;

        var orchestrationId = await starter.StartNewAsync("O_ProcessTradesFromEventHub", eventData);

        await partitionContext.CheckpointAsync();
    }

The orchestrator function is receiving then the eventData:

    [FunctionName("O_ProcessTradesFromEventHub")]
    public static async Task ProcessTradesFromEventHub([OrchestrationTrigger] DurableOrchestrationContext context,
        ILogger log)
    {
        if (!context.IsReplaying)
            Console.WriteLine("O_ProcessTradesFromEventHub is triggered");

        var eventData = context.GetInput<EventData>();

        //do stuff...
    }

But by execution of context.GetInput() I get an exception:

Function 'O_ProcessTradesFromEventHub (Orchestrator)' failed with an error. Reason: Newtonsoft.Json.JsonSerializationException: 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 'Body', line 1, position 81.

2

2 Answers

1
votes

I can think of 3 possible solutions that you can try:

  • 1 - Wrap EventData in your own class with a constructor (possibly via inheritance?).
  • 2 - Try casting to object, doubt this will work but, but worth a try as it's a simple fix.
  • 3 - Build your own DTO (Data Transfer Object) to transform EventData to <your class> and then pass <your class> to the orchestration.

I think (3) is the cleanest solution and you have full control over what you pass, unfortunately it is likely the least performant and most tedious.

Good luck!

0
votes

Use LINQ to JSON - a year later but hopefully it'll save somebody else some time.

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public static async Task Run(
    [OrchestrationTrigger] DurableOrchestrationContext context, ILogger log) {

    var eventData = context.GetInput<JObject>();
    log.LogInformation ($"Executing tasks with eventData = {eventData}");

    string step = (string)eventData.SelectToken("Step");
    log.LogInformation ($"Step = {step}");

}