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:
- type: Set to- serviceBusTrigger.
- connection: The name of an application setting that contains the Service Bus connection string.
- queueName: The name of the Service Bus queue to monitor. (Mutually exclusive with- topicNameand- subscriptionName).
- topicName: The name of the Service Bus topic to monitor. (Mutually exclusive with- queueName).
- subscriptionName: The name of the Service Bus topic subscription to monitor. (Required when- topicNameis used).
- isSessionsEnabled: (Optional) A boolean value indicating whether to enable session handling for the trigger. Defaults to- false.
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.
local.settings.json file). Do not hardcode connection strings directly in your function code.
            Advanced Scenarios
- Topic Subscriptions: You can trigger off messages from a topic subscription by providing both topicNameandsubscriptionName.
- Message Properties: Access message properties like MessageId,CorrelationId, and custom application properties through the input binding object (available in languages like C#).
- Batch Processing: For higher throughput, consider using the batch trigger support where available. This allows your function to process multiple messages in a single invocation.
Common Issues
- Connection String Errors: Double-check the connectionsetting and the actual connection string in your application settings.
- Queue/Topic Name Mismatch: Verify that the queueNameortopicName/subscriptionNameexactly matches the names in your Service Bus namespace.
- Session Handling: If sessions are enabled, ensure your Service Bus entity is configured to support sessions.
For detailed information and language-specific examples, please refer to the official Azure Functions documentation.