Search code examples
c#azureazure-functionsazureservicebus

Azure Function using ServiceBus trigger


I'm currently trying to deploy the most basic code Visual studio creates for an azure function using a service bus trigger. Locally works correctly but when I deploy it to azure it doesn't seems to do anything, and if I try to excecute it manually i get the following error:

2024-11-15T21:35:52Z   [Information]   Received HTTP response headers after 102.8069ms - 200
2024-11-15T21:35:52Z   [Information]   End processing HTTP request after 111.4159ms - 200
2024-11-15T21:35:52Z   [Information]   Call failed with gRPC error status. Status code: 'FailedPrecondition', Message: 'LockToken 00000000-0000-0000-0000-000000000000 not found.'.
2024-11-15T21:35:52Z   [Error]   Function 'FunctionSB', Invocation id 'c8144425-6892-40fc-a415-7f235d053743': An exception was thrown by the invocation.

I'm using this variable by declaring it in the Environment variables -> Connection Strings as the image bellow shows.

As I said the code is the basic one:

[Function(nameof(FunctionSB))]
public async Task Run(
            [ServiceBusTrigger("visionsystemmessages")]
            ServiceBusReceivedMessage message,
            ServiceBusMessageActions messageActions)
{
    string content = message.Body.ToString();

    // Complete the message
    await messageActions.CompleteMessageAsync(message);
    Console.WriteLine("Success start");
}

Am I missing anything? if I didn't gave enough info please ask me


Solution

  • The issue is related to misconfiguration in the Azure Function App setup or permissions in the Service Bus namespace.

    • Verify that the queue connection string in Azure Function App > App Settings.
    • Ensure that the Queue connection string has Manage, Send, and Listen permissions for the Service Bus namespace.

    After deployment, make sure to add the Service Bus queue connection string in the function app > Environment variables > App settings as shown below.

    "ServiceBusQueue": "<queueconnestring>"
    

    enter image description here

    Now send a message to the Service Bus Queue as shown below.

    enter image description here

    After some time you can directly see the logs in the Invocation section in function app as shown below.

    enter image description here

    Code :

    using System;
    using System.Threading.Tasks;
    using Azure.Messaging.ServiceBus;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Extensions.Logging;
     
    namespace FunctionApp3
    {
        public class Function1
        {
            private readonly ILogger<Function1> _logger;
     
            public Function1(ILogger<Function1> logger)
            {
                _logger = logger;
            }
     
            [Function(nameof(Function1))]
            public async Task Run(
                [ServiceBusTrigger("kamqueue", Connection = "ServiceBusQueue")]
                ServiceBusReceivedMessage message,
                ServiceBusMessageActions messageActions)
            {
                string content = message.Body.ToString();
                await messageActions.CompleteMessageAsync(message);
                Console.WriteLine("Success start");
            }
        }
    }
    

    local.settings.json :

    {
        "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
        "ServiceBusQueue": "<queueconnestring>"
      }
    }