Azure / azure-functions-dotnet-worker

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

Unable to get PartitionContext for eventhub trigger #2293

Open Riaan0berholster opened 8 months ago

Riaan0berholster commented 8 months ago

Description

Description Using an EventHub triggered function with PartitionContext as parameter causes an error:

Exception: Microsoft.Azure.Functions.Worker.FunctionInputConverterException: Error converting 1 input parameters for Function 'LanderInitial': Cannot convert input parameter 'partitionContext' to type 'Azure.Messaging.EventHubs.Consumer.PartitionContext' from type 'System.String'. Error:System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Azure.Messaging.EventHubs.Consumer.PartitionContext'. Path: $ | LineNumber: 0 | BytePositionInLine: 1.

[Function("LanderInitial")]
[FixedDelayRetry(1, "00:00:30")]
public async Task Run(
    [EventHubTrigger(
        eventHubName: "ingestion",
        Connection = "IngestionEventHubConnStr",
        ConsumerGroup = "lander-initial",
        IsBatched = false)]
    EventData myEventHubMessage,
    Microsoft.Azure.WebJobs.ExecutionContext exeContext,
    PartitionContext partitionContext)
{

The versions I am using are as follows:

<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.EventHubs" Version="6.1.0" />

Steps to reproduce

Try to use PartitionContext as parameter in azure function with eventhub trigger

Riaan0berholster commented 8 months ago

Current workaround:

public async Task Run(
    [EventHubTrigger(
        eventHubName: "ingestion",
        Connection = "IngestionEventHubConnStr",
        ConsumerGroup = "lander-initial",
        IsBatched = false)]
    EventData myEventHubMessage,
    FunctionContext functionContext,
    string partitionContext)
{
    var partitionContextDeserialized = JsonConvert.DeserializeObject<dynamic>(partitionContext);
dmunch commented 8 months ago

I've been hitting the same issue. The workaround worked partially for me, the JSON contains PartitionId, ConsumerGroup, EventHubName and FullyQualifiedNamespace.

Instead of binding to a string type and deserialising in the function you can also directly bind to a record with those fields, i.e.

public record PartitionContext(string? PartitionId, string? ConsumerGroup, string? EventHubName, string? FullyQualifiedNamespace);