Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
ServiceBusTriggerattribute to specify the Azure Service Bus instance. - For queues,
ServiceBusTriggerrequires the queue name, and the name of the setting that has connection information. - For topics,
ServiceBusTriggerrequires 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
ICollectororIAsyncCollectorallows you to output multiple messages to a Service Bus queue or topic.
Read more
- Azure Functions Service Bus bindings
- Service Bus Messaging documentations
- Service Bus client samples
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 contentMessageId- User-defined identifierSize- Size of the message in bytesExpiresAtUtc- 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
BrokeredMessageif 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 payloadout stringout 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
ServiceBusattribute to specify the output binding to an Azure Service Bus Queue. - The
ServiceBusattribute requires the name of the queue, anEntityType, 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
outkeyword.