Microsoft.Azure.WebJobs.Extensions.ServiceBus 5.0.0-beta.6

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
.NET Standard 2.0
This is a prerelease version of Microsoft.Azure.WebJobs.Extensions.ServiceBus.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Microsoft.Azure.WebJobs.Extensions.ServiceBus --version 5.0.0-beta.6
NuGet\Install-Package Microsoft.Azure.WebJobs.Extensions.ServiceBus -Version 5.0.0-beta.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.0.0-beta.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Microsoft.Azure.WebJobs.Extensions.ServiceBus --version 5.0.0-beta.6
#r "nuget: Microsoft.Azure.WebJobs.Extensions.ServiceBus, 5.0.0-beta.6"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Microsoft.Azure.WebJobs.Extensions.ServiceBus as a Cake Addin
#addin nuget:?package=Microsoft.Azure.WebJobs.Extensions.ServiceBus&version=5.0.0-beta.6&prerelease

// Install Microsoft.Azure.WebJobs.Extensions.ServiceBus as a Cake Tool
#tool nuget:?package=Microsoft.Azure.WebJobs.Extensions.ServiceBus&version=5.0.0-beta.6&prerelease

Azure WebJobs Service Bus client library for .NET

This extension provides functionality for accessing Azure Service Bus from an Azure Function.

Getting started

Install the package

Install the Service Bus extension with NuGet:

dotnet add package Microsoft.Azure.WebJobs.Extensions.ServiceBus --version 5.0.0-beta.1

Prerequisites

  • Azure Subscription: To use Azure services, including Azure Service Bus, you'll need a subscription. If you do not have an existing Azure account, you may sign up for a free trial or use your Visual Studio Subscription benefits when you create an account.

  • Service Bus namespace: To interact with Azure Service Bus, you'll also need to have a namespace available. If you are not familiar with creating Azure resources, you may wish to follow the step-by-step guide for creating a Service Bus namespace using the Azure portal. There, you can also find detailed instructions for using the Azure CLI, Azure PowerShell, or Azure Resource Manager (ARM) templates to create a Service bus entity.

To quickly create the needed Service Bus resources in Azure and to receive a connection string for them, you can deploy our sample template by clicking:

Deploy to Azure

Authenticate the Client

For the Service Bus client library to interact with a queue or topic, it will need to understand how to connect and authorize with it. The easiest means for doing so is to use a connection string, which is created automatically when creating a Service Bus namespace. If you aren't familiar with shared access policies in Azure, you may wish to follow the step-by-step guide to get a Service Bus connection string.

The Connection property of ServiceBusAttribute and ServiceBusTriggerAttribute is used to specify the configuration property that stores the connection string. If not specified, the property AzureWebJobsServiceBus is expected to contain the connection string.

For local development, use the local.settings.json file to store the connection string:

{
  "Values": {
    "<connection_name>": "Endpoint=sb://<service_bus_namespace>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<access key>"
  }
}

When deployed, use the application settings to set the connection string.

Managed identity authentication

If your environment has managed identity enabled you can use it to authenticate the Service Bus extension. To use managed identity provide the <connection_name>__fullyQualifiedNamespace configuration setting.

{
  "Values": {
    "<connection_name>__fullyQualifiedNamespace": "<service_bus_namespace>.servicebus.windows.net"
  }
}

Or in the case of deployed app set the same setting in application settings:

<connection_name>__fullyQualifiedNamespace=<service_bus_namespace>.servicebus.windows.net

Key concepts

Service Bus Trigger

The Service Bus Trigger allows a function to be executed when a message is sent to a Service Bus queue or topic.

Please follow the Azure Service Bus trigger tutorial to learn more about Service Bus triggers.

Service Bus Output Binding

The Service Bus Output Binding allows a function to send Service Bus messages.

Please follow the Azure Service Bus output binding to learn more about Service Bus bindings.

Examples

Sending individual messages

You can send individual messages to a queue or topic by applying the ServiceBus attribute to the function return value. The return value can be of type string, byte[], or ServiceBusMessage.

[FunctionName("BindingToReturnValue")]
[return: ServiceBus("<queue_or_topic_name>", Connection = "<connection_name>")]
public static string BindToReturnValue([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer)
{
    // This value would get stored in Service Bus message body.
    // The string would be UTF8 encoded.
    return $"C# Timer trigger function executed at: {DateTime.Now}";
}

You can also use an out parameter of type string, byte[], or ServiceBusMessage.

[FunctionName("BindingToOutputParameter")]
public static void Run(
[TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
[ServiceBus("<queue_or_topic_name>", Connection = "<connection_name>")] out ServiceBusMessage message)
{
    message = new ServiceBusMessage($"C# Timer trigger function executed at: {DateTime.Now}");
}

Sending multiple messages

To send multiple messages from a single Azure Function invocation you can apply the ServiceBus attribute to the IAsyncCollector<string> or IAsyncCollector<ServiceBusReceivedMessage> parameter.

[FunctionName("BindingToCollector")]
public static async Task Run(
    [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
    [ServiceBus("<queue_or_topic_name>", Connection = "<connection_name>")] IAsyncCollector<ServiceBusMessage> collector)
{
    // IAsyncCollector allows sending multiple messages in a single function invocation
    await collector.AddAsync(new ServiceBusMessage(new BinaryData($"Message 1 added at: {DateTime.Now}")));
    await collector.AddAsync(new ServiceBusMessage(new BinaryData($"Message 2 added at: {DateTime.Now}")));
}

Using binding to strongly-typed models

To use strongly-typed model classes with the ServiceBus binding apply the ServiceBus attribute to the model parameter. Doing so will attempt to deserialize the ServiceBusMessage.Bodyinto the strongly-typed model.

[FunctionName("TriggerSingleModel")]
public static void Run(
    [ServiceBusTrigger("<queue_name>", Connection = "<connection_name>")] Dog dog,
    ILogger logger)
{
    logger.LogInformation($"Who's a good dog? {dog.Name} is!");
}

Sending multiple messages using ServiceBusSender

You can also bind to the ServiceBusSender directly to have the most control over message sending.

[FunctionName("BindingToSender")]
public static async Task Run(
    [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
    [ServiceBus("<queue_or_topic_name>", Connection = "<connection_name>")] ServiceBusSender sender)
{
    await sender.SendMessagesAsync(new[]
    {
        new ServiceBusMessage(new BinaryData($"Message 1 added at: {DateTime.Now}")),
        new ServiceBusMessage(new BinaryData($"Message 2 added at: {DateTime.Now}"))
    });
}

Per-message triggers

To run a function every time a message is sent to a Service Bus queue or subscription apply the ServiceBusTrigger attribute to a string, byte[], or ServiceBusReceivedMessage parameter.

[FunctionName("TriggerSingle")]
public static void Run(
    [ServiceBusTrigger("<queue_name>", Connection = "<connection_name>")] string messageBodyAsString,
    ILogger logger)
{
    logger.LogInformation($"C# function triggered to process a message: {messageBodyAsString}");
}

Batch triggers

To run a function for a batch of received messages apply the ServiceBusTrigger attribute to a string[], or ServiceBusReceivedMessage[] parameter.

[FunctionName("TriggerBatch")]
public static void Run(
    [ServiceBusTrigger("<queue_name>", Connection = "<connection_name>")] ServiceBusReceivedMessage[] messages,
    ILogger logger)
{
    foreach (ServiceBusReceivedMessage message in messages)
    {
        logger.LogInformation($"C# function triggered to process a message: {message.Body}");
        logger.LogInformation($"EnqueuedTime={message.EnqueuedTime}");
    }
}

Message settlement

You can configure messages to be automatically completed after your function executes using the ServiceBusOptions. If you want more control over message settlement, you can bind to the MessageActions with both per-message and batch triggers.

[FunctionName("BindingToMessageActions")]
public static async Task Run(
    [ServiceBusTrigger("<queue_name>", Connection = "<connection_name>")]
    ServiceBusReceivedMessage[] messages,
    ServiceBusMessageActions messageActions)
{
    foreach (ServiceBusReceivedMessage message in messages)
    {
        if (message.MessageId == "1")
        {
            await messageActions.DeadLetterMessageAsync(message);
        }
        else
        {
            await messageActions.CompleteMessageAsync(message);
        }
    }
}

Troubleshooting

Please refer to Monitor Azure Functions for troubleshooting guidance.

Next steps

Read the introduction to Azure Functions or creating an Azure Function guide.

Contributing

See our CONTRIBUTING.md for details on building, testing, and contributing to this library.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (19)

Showing the top 5 NuGet packages that depend on Microsoft.Azure.WebJobs.Extensions.ServiceBus:

Package Downloads
MassTransit.WebJobs.ServiceBus The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

MassTransit Azure WebJobs Service Bus support; MassTransit is a message-based distributed application framework for .NET http://masstransit-project.com

CommentEverythingServiceBusConnectorNETCore

Connector for Microsoft Service Bus (Read and write to Topics and Queues). Makes reading and writing easier via batching and standardized methods.

NServiceBus.AzureFunctions.InProcess.ServiceBus The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

NServiceBus.AzureFunctions.InProcess.ServiceBus

Microsoft.Azure.Workflows.WebJobs.Extension The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Extensions for running workflows in Azure Functions

UnitTestEx

UnitTestEx Test Extensions.

GitHub repositories (13)

Showing the top 5 popular GitHub repositories that depend on Microsoft.Azure.WebJobs.Extensions.ServiceBus:

Repository Stars
MassTransit/MassTransit
Distributed Application Framework for .NET
Azure-Samples/cognitive-services-speech-sdk
Sample code for the Microsoft Cognitive Services Speech SDK
Azure/azure-webjobs-sdk
Azure WebJobs SDK
mspnp/cloud-design-patterns
Prescriptive Architecture Guidance for Cloud Applications
mspnp/reference-architectures
templates and scripts for deploying Azure Reference Architectures
Version Downloads Last updated
5.8.1 61,297 11/10/2022
5.8.0 166,507 10/11/2022
5.7.0 517,499 8/12/2022
5.6.0 135,863 7/28/2022
5.5.1 439,232 6/8/2022
5.5.0 261,977 5/17/2022
5.4.0 258,308 5/11/2022
5.3.0 756,337 3/9/2022
5.2.0 938,193 12/9/2021
5.1.0 326,230 11/11/2021
5.0.0 140,691 10/26/2021
5.0.0-beta.6 77,379 9/8/2021
5.0.0-beta.5 113,017 7/9/2021
5.0.0-beta.4 30,728 6/22/2021
5.0.0-beta.3 30,260 5/19/2021
5.0.0-beta.2 22,575 4/9/2021
5.0.0-beta.1 27,632 3/24/2021
4.3.1 67,865 6/3/2022
4.3.0 2,473,679 4/28/2021
4.2.2 234,501 4/14/2021
4.2.1 1,912,833 1/25/2021
4.2.0 1,602,944 9/23/2020
4.1.2 1,648,260 6/1/2020
4.1.1 1,135,096 3/3/2020
4.1.0 1,946,690 1/3/2020
4.0.0 334,865 11/19/2019
3.2.0 265,963 10/29/2019
3.1.1 384,178 10/1/2019
3.1.0 246,105 9/19/2019
3.1.0-beta4 15,908 8/5/2019
3.1.0-beta3 24,772 6/5/2019
3.1.0-beta2 5,035 5/10/2019
3.1.0-beta1 1,686 5/2/2019
3.0.6 815,415 6/26/2019
3.0.5 332,228 5/3/2019
3.0.4 199,959 3/29/2019
3.0.3 867,128 1/25/2019
3.0.2 205,389 12/5/2018
3.0.1 230,192 10/17/2018
3.0.0 434,150 9/19/2018
3.0.0-rc1 4,105 9/14/2018