Search code examples
c#azureservicebusazure-servicebus-queues

How add parameters (e.g. messageId, enqueuedTimeUtc) to ServiceBusTrigger when receiving Batch?


I receive messages in batch in a Service Bus queue and it works:

[FunctionName("TelemetryListenerCalculateAlgoPostNewState")]
    public static async Task TelemetryListenerCalculateAlgoPostNewState(
        [ServiceBusTrigger(busTelemetryQueueName,Connection = "ServiceBusConnection")]
        string [] receivedTelemetryMessages,
        ILogger log)
    {

But I'd like to receive further information for each message or the batch, at a minimum the enqueuedTimeUtc or messageId to calculate lag and a way to keep an eye on ordering like so: if I add such parameters to the above function I get error:

[FunctionName("ServiceBusQueueTriggerCSharp")]                    
     public static void Run(
     [ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] 
     string myQueueItem,
     Int32 deliveryCount,
     DateTime enqueuedTimeUtc,
     string messageId,
     ILogger log)
 {
     log.LogInformation($"C# ServiceBus queue trigger function processed message: 
  {myQueueItem}");
     log.LogInformation($"EnqueuedTimeUtc={enqueuedTimeUtc}");
     log.LogInformation($"DeliveryCount={deliveryCount}");
     log.LogInformation($"MessageId={messageId}");
 }

[2022-11-23T18:11:02.038Z] Microsoft.Azure.WebJobs.Host: Error indexing method 'TelemetryListenerCalculateAlgoPostNewState'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'enqueuedTimeUtc' to type DateTime. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

How can I achieve this when receiving a batch with ServiceBusTrigger? I didn't use topics because i'm using only this consumer.

Thank you


Solution

  • In case it helps someone, Application Insights logs gave me a clue:

    Trigger Details: MessageIdArray: 1566b78362294301acd862fd245d322e,fbe200204cbc48ddb9e3d8d4b196bc82, SequenceNumberArray: 1692,1693, DeliveryCountArray: 1,1, EnqueuedTimeUtcArray: 2022-11-23T18:23:22.4200000+00:00,2022-11-23T18:23:22.4200000+00:00, LockedUntilArray: 2022-11-23T18:28:22.4360000+00:00,2022-11-23T18:28:22.4360000+00:00, SessionId: (null)

    and this worked:

    [FunctionName("TelemetryListenerCalculateAlgoPostNewState")]
        public static async Task TelemetryListenerCalculateAlgoPostNewState(
            [ServiceBusTrigger(busTelemetryQueueName,Connection = "ServiceBusConnection")]
            string [] receivedTelemetryMessages,
            Int32 [] DeliveryCountArray,
            DateTime [] EnqueuedTimeUtcArray,
            string [] MessageIdArray,
            Int64[] SequenceNumberArray,
            ILogger log)
        {