Azure Functions with Azure Service Bus

Intro

Azure Functions can be executed via an Azure Service Bus trigger. In this post I provide an example for how I was able to publish a message to a queue as well as a topic.

AppConfig

An app config file is great for hosting connection strings as well as other key/value pairs.

Here’s an example of my AppConfig file:















Queue a Message

The following code depicts how to publish a message to the queue:


let ``Queue servicebus message``() =

    // Setup
    let connectionstring = ConfigurationManager.ConnectionStrings.["servicebus_testEnv"].ConnectionString

    // Test
    async {

        let client  = QueueClient(connectionstring, "QueueFor.subscribe")
        let data    = "test_data"
        let message = Message(Encoding.UTF8.GetBytes(data))

        do! client.SendAsync(message) |> Async.AwaitTask
        do! client.CloseAsync()       |> Async.AwaitTask
    }

ServiceBusTrigger for Queued Message

The following code depicts the service bus trigger used for consuming the message:

    public static class SubscribeFn
    {
        [FunctionName(nameof(SubscribeFn))]
        public static async void Run([ServiceBusTrigger("QueueFor.subscribe",
                                                        Connection= "MyConnectionKey")]string myQueueItem, ILogger log)
        {
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
        }
    }

Queue a Topic Message

The following code depicts how to publish a topic message to the queue:

[Fact]
let ``Publish message assigned to servicebus topic``() =

    // Setup
    let connectionstring = ConfigurationManager.ConnectionStrings.["servicebus_testEnv"].ConnectionString

    // Test
    async {

        let client  = TopicClient(connectionstring, "TopicFor.subscribe")
        let data    = "test_data"
        let message = Message(Encoding.UTF8.GetBytes(data))

        do! client.SendAsync(message) |> Async.AwaitTask
        do! client.CloseAsync()       |> Async.AwaitTask
    }

ServiceBusTrigger for Topic  Message

The following code depicts the service bus trigger used for consuming the message:

    public static class SubscribeFn
    {
        [FunctionName(nameof(SubscribeFn))]
        public static async void Run([ServiceBusTrigger("TopicFor.subscribe", "default-subscription",
                                                        Connection= "MyConnectionKey")]string myQueueItem, ILogger log)
        {
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
        }
    }

ReceiveMode

The receive mode is set to PeekLock by default. Thus, the message is dequeued post successful execution of an Azure Function.

Conclusion

In conclusion, I provided a couple of examples of Azure Functions support for Azure Service Bus.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: