Service Bus Bindings for Azure Functions

Azure Functions provides flexible bindings for interacting with Azure Service Bus queues and topics. These bindings enable your functions to send messages to, receive messages from, and respond to Service Bus entities with minimal code.

Supported Service Bus Entities

Azure Functions supports the following Service Bus entities:

Input Bindings (Receiving Messages)

You can configure your function to be triggered by messages arriving on a Service Bus queue or topic subscription. This is typically done using the ServiceBusTrigger attribute.

Example: Service Bus Queue Trigger

This example shows a C# function triggered by messages in a Service Bus queue named "myqueue". The message content is passed as a string to the function.

// function.json
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "message",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "myqueue",
      "connection": "ServiceBusConnection"
    }
  ]
}

import logging
import azure.functions as func

def main(message: func.ServiceBusMessage):
    logging.info('Python ServiceBus queue trigger function processed message: %s',
                 message.get_body().decode('utf-8'))

Example: Service Bus Topic Trigger

This example shows a C# function triggered by messages on a topic subscription. The message content is passed as a string.

// function.json
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "message",
      "type": "serviceBusTrigger",
      "direction": "in",
      "topicName": "mytopic",
      "subscriptionName": "mysubscription",
      "connection": "ServiceBusConnection"
    }
  ]
}

import logging
import azure.functions as func

def main(message: func.ServiceBusMessage):
    logging.info('Python ServiceBus topic trigger function processed message: %s',
                 message.get_body().decode('utf-8'))

Output Bindings (Sending Messages)

You can send messages to Service Bus queues or topics from within your function using output bindings. This is typically done with the ServiceBus attribute.

Example: Sending to a Service Bus Queue

This example shows a C# function that receives an HTTP request and sends a message to a Service Bus queue named "outputqueue".

// function.json
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "name": "outputQueue",
      "type": "serviceBus",
      "direction": "out",
      "queueName": "outputqueue",
      "connection": "ServiceBusConnection"
    }
  ]
}

import logging
import azure.functions as func

def main(req: func.HttpRequest, outputQueue: func.Out[str]) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    message_body = req.get_json().get('message', 'Default message')

    outputQueue.set(message_body)

    return func.HttpResponse(
        "Message sent to Service Bus queue.",
        status_code=200
    )

Connection Strings

The connection property in the binding configuration refers to a name in your application's settings (e.g., local.settings.json or Azure App Settings) that holds the Service Bus connection string.

Example local.settings.json:


{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "ServiceBusConnection": "Endpoint=sb://.servicebus.windows.net/;SharedAccessKeyName=;SharedAccessKey="
  }
}
            

Important Considerations

  • Ensure you have the correct Service Bus connection string with appropriate permissions.
  • The connection name must match the key in your application settings.
  • For topics, remember to specify both topicName and subscriptionName for triggers.

Advanced Scenarios

Message Properties

When receiving messages, you can access various message properties like message_id, correlation_id, and user_properties.


import logging
import azure.functions as func

def main(message: func.ServiceBusMessage):
    logging.info('Message ID: %s', message.message_id)
    logging.info('Correlation ID: %s', message.correlation_id)
    logging.info('User Properties: %s', message.user_properties)
    logging.info('Body: %s', message.get_body().decode('utf-8'))

Message Sessions

Azure Functions can process Service Bus messages within sessions. You can configure session-enabled triggers and handle sessions in your code.

To enable session processing, set isSessionsEnabled to true in your trigger binding configuration.

// function.json for session-enabled trigger
{
  "name": "sessionMessage",
  "type": "serviceBusTrigger",
  "direction": "in",
  "queueName": "mysessionqueue",
  "connection": "ServiceBusConnection",
  "isSessionsEnabled": true
}

Tip

Use the ServiceBusMessage object in your function parameters for richer interaction with Service Bus messages.

Further Reading