Azure Functions & Service Bus Bindings

Effortless integration for scalable messaging

Introduction

Azure Functions provide robust bindings for Azure Service Bus, allowing you to easily trigger functions from Service Bus messages and send messages to Service Bus queues or topics. This integration simplifies building event-driven, decoupled applications.

Service Bus bindings abstract away much of the complexity of interacting with Service Bus, making it straightforward to implement scenarios like:

  • Processing messages from a queue.
  • Handling messages sent to a topic.
  • Sending messages to a queue or topic as an output.
  • Implementing durable messaging patterns.

Trigger Bindings

Trigger bindings allow your function to be executed when a new message arrives in a Service Bus queue or topic subscription.

ServiceBusTrigger

This is the primary trigger for Service Bus integration. It listens for messages.

Key Attributes:

  • queueName or topicName and subscriptionName: Specifies the Service Bus entity to listen to.
  • connection: The name of the app setting that contains the Service Bus connection string.
  • isSessionsEnabled: (Optional) Set to true if using sessions.

[FunctionName("ProcessServiceBusMessage")]
public static void Run(
    [ServiceBusTrigger("myqueue", Connection = "ServiceBusConnectionString")] string myQueueItem,
    ILogger log)
{
    log.LogInformation($"C# ServiceBus queue trigger function processed: {myQueueItem}");
}
                    
When using topic subscriptions, you'll configure both topicName and subscriptionName.

Input Bindings

Input bindings allow you to retrieve a single message from a Service Bus queue or topic as input to your function. This is useful for fetching specific messages based on a trigger or direct invocation.

ServiceBus (Input)

Use this binding to read a single message.

Key Attributes:

  • queueName or topicName and subscriptionName: The Service Bus entity to read from.
  • connection: The app setting for the Service Bus connection string.

[FunctionName("ProcessWithInputBinding")]
public static void Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req,
    [ServiceBus("myqueue", Connection = "ServiceBusConnectionString")] string serviceBusMessage,
    ILogger log)
{
    log.LogInformation($"Message from Service Bus: {serviceBusMessage}");
    // Process the message...
}
                    
Input bindings retrieve one message at a time. For processing multiple messages, use the ServiceBusTrigger.

Output Bindings

Output bindings enable your function to send messages to a Service Bus queue or topic.

ServiceBus (Output)

This binding allows you to send messages as a function output.

Key Attributes:

  • queueName or topicName: The Service Bus entity to send messages to.
  • connection: The app setting for the Service Bus connection string.

[FunctionName("SendMessageToServiceBus")]
public static void Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req,
    [ServiceBus("outputqueue", Connection = "ServiceBusConnectionString", IsBatched = false)] out string outputQueueItem,
    ILogger log)
{
    string message = "Hello from Azure Functions!";
    outputQueueItem = message;
    log.LogInformation($"Sent message to Service Bus: {message}");
}
                    
The IsBatched = false property explicitly indicates that a single message should be sent. For batched sending, this would be true, and the parameter type would be out string[] or similar collection.

Advanced Scenarios

Message Properties

You can access and set message properties (like Correlation ID, Message ID, etc.) by using the Microsoft.Azure.ServiceBus.Message type for triggers and outputs.


[FunctionName("ProcessWithProperties")]
public static void Run(
    [ServiceBusTrigger("myqueue", Connection = "ServiceBusConnectionString")] Message myQueueMessage,
    [ServiceBus("outputqueue", Connection = "ServiceBusConnectionString")] out Message outputMessage,
    ILogger log)
{
    log.LogInformation($"Processing message with ID: {myQueueMessage.MessageId}");
    outputMessage = new Message($"Processed: {myQueueMessage.Body}");
    outputMessage.CorrelationId = myQueueMessage.MessageId;
}
                    

Session Support

If your Service Bus entities are session-enabled, you can configure your trigger or binding to handle sessions by setting isSessionsEnabled = true.

Error Handling & Retries

Azure Functions integrate with Azure Functions runtime's retry policies. Unhandled exceptions in a triggered function will result in the message being returned to the queue/topic for reprocessing, up to the configured retry limits.

Configuration

Connection strings for Service Bus are typically stored in your Azure Function App's application settings. The connection attribute in the bindings refers to the name of this setting.

Example App Setting:


{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "ServiceBusConnectionString": "Endpoint=sb://your-namespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=YOUR_KEY"
  }
}
                    
Always store connection strings securely. Use Azure Key Vault for production environments.