Service Bus Triggers and Bindings Samples

Triggering functions from Azure Service Bus Queues and Topics

Queues and Topics from Azure Service Bus can be used as triggers for your functions.

Using the ServiceBusTrigger attribute, you can supply the queue or topic name, along with the connection information for the Service Bus instance.

 [FunctionName("ServiceBusQueueTrigger")]
 public static void Run([ServiceBusTrigger("funcqueue", AccessRights.Manage, Connection = "ConnectionSetting")]string queueMessage,
                        TraceWriter log)
 {
     log.Info("101 Azure Function Demo - Service Bus Queue Trigger");

     log.Info($"C# ServiceBus queue trigger function processed message: {queueMessage}");
 }

Topic Trigger

[FunctionName("ServiceBusTopicTrigger")]
public static void RunTopic([ServiceBusTrigger("functopic", "sampletopic", AccessRights.Manage, Connection = "ConnectionSetting")]string topicMessage,
                             TraceWriter log)
{
    log.Info("101 Azure Function Demo - Service Bus Topic Trigger");

    log.Info($"C# ServiceBus topic trigger function processed message: {topicMessage}");
}

Takeaways

  • Use the ServiceBusTrigger attribute to specify the Azure Service Bus instance.
  • For queues, ServiceBusTrigger requires the queue name, and the name of the setting that has connection information.
  • For topics, ServiceBusTrigger requires the topic name, subscription name, and the name of the setting that has connection information.

Read more

Using ICollector with Service Bus queue bindings

ICollector and IAsyncCollector can be used as parameter types for Azure Service Bus output bindings. Using these interfaces allows you to add multiple messages to the respective Service Bus queue or topic.

[FunctionName("CollectorQueueOutput")]
public static void Run([TimerTrigger("*/10 * * * * *")]TimerInfo myTimer,
                       TraceWriter log,
                     [ServiceBus("funcqueue", Connection = "ConnectionSetting", EntityType = EntityType.Queue)] ICollector<string> queueCollector)
{
    log.Info("101 Azure Function Demo - Azure Service Bus output");

    queueCollector.Add(DateTime.UtcNow.ToString());
    queueCollector.Add("Sample");
    queueCollector.Add("Message");
}        

Takeaways

  • Using ICollector or IAsyncCollector allows you to output multiple messages to a Service Bus queue or topic.

Read more

Retrieving queue metadata from an Azure Service Bus Queue/Topic Trigger

With the Service Bus Trigger, the BrokeredMessage class can be used to retrieve metadata about the queue message.

Some common properties include:

  • ContentType - Type of the message content
  • MessageId - User-defined identifier
  • Size - Size of the message in bytes
  • ExpiresAtUtc - Date and time in UTC when the message expires
[FunctionName("ServiceBusQueuesTrigger")]
public static void Run([ServiceBusTrigger("funcqueue", AccessRights.Manage, Connection = "Func101SB")]BrokeredMessage queueMessage,
                        TraceWriter log)
{
    log.Info("101 Azure Function Demo - Service Bus Queue Trigger");

    log.Info($"Message ID: {queueMessage.MessageId}");
    log.Info($"Message Content Type: {queueMessage.ContentType}");
}

Takeaways

  • Use BrokeredMessage if you need access to properties of the input message.

Read more

Azure Service Bus output binding

Azure Service Bus can be used as output bindings for your Azure Functions app. Using the ServiceBus attribute allows you to take a parameter as a source to push queue messages. Some supported parameter types include:

  • out <POCO> - a .NET object is serialized into JSON before being added to the message payload
  • out string
  • out byte[]
[FunctionName("ServiceBusOutput")]
public static void Run([TimerTrigger("0/10 * * * * *")]TimerInfo myTimer,
                       TraceWriter log,
                       [ServiceBus("funcqueue", Connection = "ConnectionSetting", EntityType = EntityType.Queue)]out string queueMessage)
{
    log.Info("101 Azure Function Demo - Azure Service Bus Queue output");

    queueMessage = DateTime.UtcNow.ToString();
}

Takeaways

  • Use the ServiceBus attribute to specify the output binding to an Azure Service Bus Queue.
  • The ServiceBus attribute requires the name of the queue, an EntityType, and the name of the setting for the Azure Service Bus connection string.
  • If POCOs, strings, and byte arrays are parameters of your function, they must be marked with the out keyword.

Read more