Skip to content

Service Bus

Consume Service Bus queue and topic messages with the trigger and publish messages with the output binding in the .NET isolated worker model.

flowchart TD
    A[Producers] --> B[(Service Bus Queue/Topic)]
    B --> C[ServiceBusTrigger Function]
    C --> D[(Dead-Letter Queue)]
    C --> E[ServiceBusOutput]

Topic/Command Groups

Queue trigger

Message settlement is automatic: returning settles the message, and throwing abandons it. After maxDeliveryCount the message is dead-lettered.

[Function("ProcessOrder")]
public void ProcessOrder(
    [ServiceBusTrigger("orders", Connection = "ServiceBusConnection")] string message,
    FunctionContext context)
{
    var logger = context.GetLogger("ProcessOrder");
    logger.LogInformation("Processing message: {Message}", message);
}

Topic/subscription trigger

[Function("ProcessEvent")]
public void ProcessEvent(
    [ServiceBusTrigger("events", "billing", Connection = "ServiceBusConnection")] string message,
    FunctionContext context)
{
    context.GetLogger("ProcessEvent").LogInformation("Subscription message: {Message}", message);
}

Output binding

[Function("Enqueue")]
[ServiceBusOutput("orders", Connection = "ServiceBusConnection")]
public string Enqueue(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = "servicebus/enqueue")]
    HttpRequestData request)
{
    return new StreamReader(request.Body).ReadToEnd();
}

Identity-based connection

az functionapp config appsettings set \
  --name $APP_NAME \
  --resource-group $RG \
  --settings "ServiceBusConnection__fullyQualifiedNamespace=$NAMESPACE.servicebus.windows.net"
| Command/Parameter | Purpose | | --- | --- | | az functionapp config appsettings set | Add or update application settings on the function app. | | --name | Name of the target resource. | | --resource-group | Resource group that contains the resource. | | --settings | Key=value application settings to apply. |

Grant the managed identity the Azure Service Bus Data Receiver (and Data Sender for output) role.

Host settings

{
  "version": "2.0",
  "extensions": {
    "serviceBus": {
      "maxConcurrentCalls": 16,
      "prefetchCount": 0,
      "maxAutoLockRenewalDuration": "00:05:00"
    }
  }
}

Review Matrix

Review area Page-specific check
Scope Confirm the guidance applies to Service Bus trigger and output bindings.
Source basis Validate the recommendation against the Microsoft Learn sources in this page.
Evidence Capture command output, portal state, metrics, logs, or screenshots before treating the result as proven.

See Also

Sources