Azure Service Bus output binding for Azure Functions
Use Azure Service Bus output binding to send queue or topic messages.
For information on setup and configuration details, see the overview.
Example
The following example shows a C# function that sends a Service Bus queue message:
[FunctionName("ServiceBusOutput")]
[return: ServiceBus("myqueue", Connection = "ServiceBusConnection")]
public static string ServiceBusOutput([HttpTrigger] dynamic input, ILogger log)
{
log.LogInformation($"C# function processed: {input.Text}");
return input.Text;
}
Attributes and annotations
In C# class libraries, use the ServiceBusAttribute.
The attribute's constructor takes the name of the queue or the topic and subscription. You can also specify the connection's access rights. How to choose the access rights setting is explained in the Output - configuration section. Here's an example that shows the attribute applied to the return value of the function:
[FunctionName("ServiceBusOutput")]
[return: ServiceBus("myqueue")]
public static string Run([HttpTrigger] dynamic input, ILogger log)
{
...
}
You can set the Connection
property to specify the name of an app setting that contains the Service Bus connection string to use, as shown in the following example:
[FunctionName("ServiceBusOutput")]
[return: ServiceBus("myqueue", Connection = "ServiceBusConnection")]
public static string Run([HttpTrigger] dynamic input, ILogger log)
{
...
}
For a complete example, see Output - example.
You can use the ServiceBusAccount
attribute to specify the Service Bus account to use at class, method, or parameter level. For more information, see Trigger - attributes.
Configuration
The following table explains the binding configuration properties that you set in the function.json file and the ServiceBus
attribute.
function.json property | Attribute property | Description |
---|---|---|
type | n/a | Must be set to "serviceBus". This property is set automatically when you create the trigger in the Azure portal. |
direction | n/a | Must be set to "out". This property is set automatically when you create the trigger in the Azure portal. |
name | n/a | The name of the variable that represents the queue or topic message in function code. Set to "$return" to reference the function return value. |
queueName | QueueName | Name of the queue. Set only if sending queue messages, not for a topic. |
topicName | TopicName | Name of the topic. Set only if sending topic messages, not for a queue. |
connection | Connection | The name of an app setting that contains the Service Bus connection string to use for this binding. If the app setting name begins with "AzureWebJobs", you can specify only the remainder of the name. For example, if you set connection to "MyServiceBus", the Functions runtime looks for an app setting that is named "AzureWebJobsMyServiceBus". If you leave connection empty, the Functions runtime uses the default Service Bus connection string in the app setting that is named "AzureWebJobsServiceBus".To obtain a connection string, follow the steps shown at Get the management credentials. The connection string must be for a Service Bus namespace, not limited to a specific queue or topic. |
accessRights (v1 only) | Access | Access rights for the connection string. Available values are manage and listen . The default is manage , which indicates that the connection has the Manage permission. If you use a connection string that does not have the Manage permission, set accessRights to "listen". Otherwise, the Functions runtime might fail trying to do operations that require manage rights. In Azure Functions version 2.x and higher, this property is not available because the latest version of the Service Bus SDK doesn't support manage operations. |
When you're developing locally, app settings go into the local.settings.json file.
Usage
In Azure Functions 1.x, the runtime creates the queue if it doesn't exist and you have set accessRights
to manage
. In Functions version 2.x and higher, the queue or topic must already exist; if you specify a queue or topic that doesn't exist, the function will fail.
Use the following parameter types for the output binding:
out T paramName
-T
can be any JSON-serializable type. If the parameter value is null when the function exits, Functions creates the message with a null object.out string
- If the parameter value is null when the function exits, Functions does not create a message.out byte[]
- If the parameter value is null when the function exits, Functions does not create a message.out BrokeredMessage
- If the parameter value is null when the function exits, Functions does not create a message (for Functions 1.x)out Message
- If the parameter value is null when the function exits, Functions does not create a message (for Functions 2.x and higher)ICollector<T>
orIAsyncCollector<T>
(for async methods) - For creating multiple messages. A message is created when you call theAdd
method.
When working with C# functions:
Async functions need a return value or
IAsyncCollector
instead of anout
parameter.To access the session ID, bind to a
Message
type and use thesessionId
property.
Exceptions and return codes
Binding | Reference |
---|---|
Service Bus | Service Bus Error Codes |
Service Bus | Service Bus Limits |
host.json settings
This section describes the global configuration settings available for this binding in versions 2.x and higher. The example host.json file below contains only the settings for this binding. For more information about global configuration settings, see host.json reference for Azure Functions version.
Note
For a reference of host.json in Functions 1.x, see host.json reference for Azure Functions 1.x.
{
"version": "2.0",
"extensions": {
"serviceBus": {
"prefetchCount": 100,
"messageHandlerOptions": {
"autoComplete": true,
"maxConcurrentCalls": 32,
"maxAutoRenewDuration": "00:05:00"
},
"sessionHandlerOptions": {
"autoComplete": false,
"messageWaitTimeout": "00:00:30",
"maxAutoRenewDuration": "00:55:00",
"maxConcurrentSessions": 16
}
}
}
}
If you have isSessionsEnabled
set to true
, the sessionHandlerOptions
will be honored. If you have isSessionsEnabled
set to false
, the messageHandlerOptions
will be honored.
Property | Default | Description |
---|---|---|
prefetchCount | 0 | Gets or sets the number of messages that the message receiver can simultaneously request. |
maxAutoRenewDuration | 00:05:00 | The maximum duration within which the message lock will be renewed automatically. |
autoComplete | true | Whether the trigger should automatically call complete after processing, or if the function code will manually call complete. Setting to false is only supported in C#.If set to true , the trigger completes the message automatically if the function execution completes successfully, and abandons the message otherwise.When set to false , you are responsible for calling MessageReceiver methods to complete, abandon, or deadletter the message. If an exception is thrown (and none of the MessageReceiver methods are called), then the lock remains. Once the lock expires, the message is re-queued with the DeliveryCount incremented and the lock is automatically renewed.In non-C# functions, exceptions in the function results in the runtime calls abandonAsync in the background. If no exception occurs, then completeAsync is called in the background. |
maxConcurrentCalls | 16 | The maximum number of concurrent calls to the callback that the message pump should initiate per scaled instance. By default, the Functions runtime processes multiple messages concurrently. |
maxConcurrentSessions | 2000 | The maximum number of sessions that can be handled concurrently per scaled instance. |