Introduction to Service Bus Bindings

Azure Functions provide built-in bindings for Azure Service Bus, enabling seamless integration with message queues and topics. These bindings simplify the process of sending and receiving messages without requiring manual SDK interactions.

Trigger Bindings (Input)

Service Bus trigger bindings allow your function to be executed automatically when a new message arrives in a Service Bus queue or topic subscription. This is an imperative way to build event-driven architectures.

Queue Trigger

The ServiceBusTrigger attribute can be used to listen for messages on a Service Bus queue. The function executes with the message content as input.

// C# Example
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.ServiceBus;
using Microsoft.Extensions.Logging;

public static class ServiceBusQueueListener
{
    [FunctionName("ServiceBusQueueTrigger")]
    public static void Run(
        [ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] Message message,
        ILogger log)
    {
        log.LogInformation($"C# ServiceBus queue trigger function processed message: {message.MessageId}");
        log.LogInformation($"Message body: {message.Body}");
    }
}

Topic Trigger (Subscription)

For Service Bus topics, you can trigger functions based on messages arriving in a topic subscription. This allows for publish-subscribe patterns.

// C# Example
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.ServiceBus;
using Microsoft.Extensions.Logging;

public static class ServiceBusTopicListener
{
    [FunctionName("ServiceBusTopicTrigger")]
    public static void Run(
        [ServiceBusTrigger("mytopic", "mysubscription", Connection = "ServiceBusConnection")] Message message,
        ILogger log)
    {
        log.LogInformation($"C# ServiceBus topic trigger function processed message: {message.MessageId}");
        log.LogInformation($"Message body: {message.Body}");
    }
}

Output Bindings

Service Bus output bindings allow your function to send messages to Service Bus queues or topics. This is useful for asynchronous communication and decoupling services.

Queue Output Binding

Use the ServiceBus attribute with the Queue type to send messages to a specified queue.

// C# Example
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.ServiceBus.Messaging;
using Microsoft.Extensions.Logging;

public static class ServiceBusQueueSender
{
    [FunctionName("ServiceBusQueueSender")]
    [return: ServiceBus("myoutputqueue", Connection = "ServiceBusConnection")]
    public static string Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] dynamic req,
        ILogger log)
    {
        string messageToSend = "Hello from Azure Functions!";
        log.LogInformation($"Sending message: {messageToSend}");
        return messageToSend;
    }
}

Topic Output Binding

Similarly, you can send messages to a Service Bus topic using the ServiceBus attribute with the Topic type.

// C# Example
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.ServiceBus.Messaging;
using Microsoft.Extensions.Logging;

public static class ServiceBusTopicSender
{
    [FunctionName("ServiceBusTopicSender")]
    [return: ServiceBus("mytopicsend", Connection = "ServiceBusConnection")]
    public static BrokeredMessage Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] dynamic req,
        ILogger log)
    {
        string messageContent = "Message for the topic!";
        log.LogInformation($"Creating message for topic: {messageContent}");
        return new BrokeredMessage(messageContent);
    }
}

Common Configuration Options

Both trigger and output bindings share several common configuration options:

Best Practices

Advanced Scenarios

For more advanced scenarios, consider using the Service Bus SDK directly within your function code for finer control over message properties, batching, and dead-letter queue management.