Service Bus Trigger Bindings

The Azure Functions Service Bus trigger allows your function to execute in response to a message arriving in a Service Bus queue or topic subscription. This binding provides a powerful and scalable way to process messages asynchronously.

Overview

When a message arrives in the configured Service Bus entity (queue or topic subscription), the trigger fires, and the function is invoked with the message payload as input. You can configure various aspects of the trigger, including the connection string, entity name, and session handling.

Configuration

The Service Bus trigger is configured in the function.json file (for C#, JavaScript, and Python) or using attributes (for C#). You need to specify:

Example: function.json (Node.js)

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "message",
      "type": "serviceBusTrigger",
      "direction": "in",
      "connection": "ServiceBusConnection",
      "queueName": "myqueue",
      "isSessionsEnabled": false
    }
  ]
}

Example: C# Attribute

using Microsoft.Azure.WebJobs;

public static class ServiceBusQueueTriggerFunction
{
    [FunctionName("ServiceBusQueueTriggerCSharp")]
    public static void Run(
        [ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] string myQueueItem,
        ILogger log)
    {
        log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
    }
}

Message Handling

The trigger automatically handles message completion upon successful function execution. If your function throws an unhandled exception, the message may be retried based on your Service Bus queue/subscription configuration (e.g., maximum delivery count, dead-lettering).

Sessions

When isSessionsEnabled is set to true, the trigger will process messages in session order. This is crucial for scenarios where you need to maintain state or process related messages sequentially.

Important: Ensure that your Service Bus connection string is stored securely in your application settings (e.g., in Azure App Settings or a local local.settings.json file). Do not hardcode connection strings directly in your function code.

Advanced Scenarios

Common Issues

For detailed information and language-specific examples, please refer to the official Azure Functions documentation.