'Azure Functions V4 // ServiceBusTrigger and binding properties like Message or MessageReceiver

I am working on an Azure Function (in-process model, V 4.1) with a ServiceBusTrigger for a queue. The function works fine and gets triggered. The function looks like this:

    public async Task Run([ServiceBusTrigger("my-queue", Connection = "myConnectionString", AutoCompleteMessages = true)]
        string myQueueItem, Int32 deliveryCount, DateTime enqueuedTimeUtc, string messageId)

I now want to be able to get the correlationId that I passed in the message and I want to be able to "Complete()" the message by myself.

I looked in the docs here (https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger?tabs=in-process%2Cextensionv5&pivots=programming-language-csharp) but did not find any way to bind other parameters like Message, MessageReceiver, etc. Whenever I try to use these, I get a binding exception when the function runs.

These are the relevant packages I am using

<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.7.0" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Core" Version="1.4.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.3.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.0" />

I must be missing something...any hint is appreciated!

EDIT: After a cleanup, these are the relevant packages:

<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.3.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="6.0.3" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.0" />


Solution 1:[1]

A (possible) solution:

Check the source code: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Triggers/ServiceBusTriggerBindingStrategy.cs#L77

Make sure to also name the parameters exactly like in the src (the "contract")

  • ServiceBusClient client -> works
  • ServiceBusClient myclient -> does not work!

This is how my function looks now:

(...,string myQueueItem, Int32 deliveryCount, DateTime enqueuedTimeUtc, string messageId, ServiceBusMessageActions messageActions, ServiceBusMessageActions messageReceiver, ServiceBusClient client)

Solution 2:[2]

I am working on an Azure Function (in-process model, V 4.1)

You're not.

<PackageReference Include="Microsoft.Azure.Functions.Worker.Core" Version="1.4.0" />

Based on the NuGet packages you're using, the SDK is out-of-process / Isolated Worker SDK. With Isolated Worker SDK you cannot complete messages yourself. That's only possible with In-Process SDK. Also, SDK specific types such as ServiceBusReceivedMessage or ServiceBusReceiver are not available either.

Saying that, you can have access to the system and application properties of the messages. You'd need to access FunctionContext in the following way:

context.BindingContext.BindingData.TryGetValue("CorrelationId", out var label)

If you need to have access to the SDK types or complete the message yourself, you need to use Azure Functions In-Process SDK, Microsoft.NET.Sdk.Functions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thread Pitt
Solution 2 Sean Feldman