Azure Functions - Service Bus Queue Trigger

By James Kenny
2 min read

Table of Contents

Azure functions allow have a service bus queue trigger, which will execute the code in the function each time a message is added to the service bus.

Create Service Bus trigger

In Azure functions create a new function, From the template options select "ServiceBusQueueTrigger - C#"

Azure Functions Service Bus Queue Trigger

Select the ServiceBus Connection you want to use, you can also select the access key you want to use. In the above I've selected my contentappMessage bus and my root key.

Select the Queue name and then create

This will create an Azure function that will run when ever a message is added to the service bus Queue you have selected.

For more on Azure Service bus check out my blog post Getting Started with Azure Service Bus

public static void Run(string myQueueItem, TraceWriter log)
{
    log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}

Each time you add a message to the service bus queue this function will execute and log the item.

But there is an issue with the trigger. string myQueueItem isn't really useful not if you have used a brokered message and added other information you want to use in the function.

So lets change it.

First lets add the nuget package for Azure Service Bus into our function. (see here for more)

In View Files add "project.json"

Azure Functions Add Project.json

In project.json add the following code


{
  "frameworks": 
    {  
      "net46":
      { 
        "dependencies":
        {
          "WindowsAzure.ServiceBus": "4.1.2"
        }
      }
    }
}

And save, watching the logs panel you will see it restoring the nuget package

Azure Functions logs

Back into our Run.csx and we want to make some changes


using System;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;

public static void Run(BrokeredMessage myMessage, TraceWriter log)
{
    log.Info($"C# ServiceBus queue trigger function processed message");

    string sMessage = myMessage.Properties["Message"].ToString();
    string sID = myMessage.Properties["ID"].ToString();


    log.Info($"message - " + sMessage + " Id " + sID);
}
 

We add the

Using Microsoft.ServiceBus.Messaging

We then change

string myQueueItem

to be

BrokeredMessage myMessage

Then Replace the log lines as this will fail. But also we add in two strings reading from our message in the service bus queue.

Lastly we need to change the binding in the function. Open Function.json in the View Files.


{
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "serversncodedemo",
      "connection": "contentappmessage_XXXXXXXXXXXXXXXXX",
      "accessRights": "Manage"
    }
  ],
  "disabled": false
}

This is the binding that will trigger the function to execute.

Change the "Name": "myQueueItem" to


"name": "myMessage"

Now save and in the logs it should now read Compilation succeeded.

Last Update: August 21, 2018

About the Author