Using an Azure Service Bus Topic Subscription in an Azure Function

This post shows how to consume Azure service bus topic subscriptions in an Azure function.


Posts in this series:


2021-05-19 Updated .NET, Azure Service Bus SDK

Processing the Azure Service Bus Messages in an Azure Function

Setting this up could not be easier. In Visual Studio, create a new Azure function project, and then create a new Azure function using the Visual Studio helpers.

Select a Service Bus Topic trigger and add the definitions as required. These can be changed later, if you don’t know the required values.

The new Azure function has an attribute which is used to defined the client for the topic subscription. The service bus connection string is defined as ServiceBusConnectionString. This will be specified later. The message is then deserialized to an object. Now you can handle the payload as required.

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace FunctionService3
    public static class MyQueueFunction
        public static void Run([ServiceBusTrigger("myqueue", Connection = "ServiceBusConnectionString")]string myQueueItem, ILogger log)
            //throw new Exception("Cannot not process for some reason");
            log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");

The connection for the Azure service bus is defined in the local.settings.json file. This is a bit of a problem because we should not be committing this secret value to our source code repository. We could use a dev service bus here, and commit this then. The test and production service bus connection strings could then be set as part of the build.

Azure Key Vault could also be used to get the service bus connection string. This is only a little bit better as the key vault secret would be pushed to the server, so we still have a secret in our code.

It would be nice if we could use something like the Microsoft extensions key vault configuration where no secret is required to use the key vault.

  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "ServiceBusConnectionString": "Not possible to read from a secret src without a another"
  "Host": {
    "LocalHttpPort": "7071",
    "CORS":  "*"

The Azure service bus subscription needs to be configured in Azure:

It is really easy to use Azure service bus with Azure functions. The handling of secrets could be improved. There are some open source projects which help solve this problem.


Azure Service Bus Topologies

Always subscribe to Dead-lettered messages when using an Azure Service Bus


  1. Schmallaria · · Reply

    Could you please share some information on how to debug azure functions? Especially those which uses ServiceBusTrigger.

    1. Hi Schmallaria
      For local dev, this should just work. The example in this blog is committed to the git repo, and it should just start. You need to install to local tools to run azure functions. Or do you mean directly on the deployment, like in Azure?

      Greetings Damien

  2. Schmallaria · · Reply

    Hi Damien,

    Yes, I mean local debugging. Could you explain what tools I need and what I have to do to start debugging? When I try to debug a Azure function with a ServiceBusTrigger I always get the error: “Cannot start the Azure Storage Emulator. Please run AzureStorageEmulator.exe start”.


    1. I added the links for the tools here:

      install the tools from the 3 links, and you will be able to debug, run locally

      Greetings Damien

      1. Schmallaria · ·

        Hi Damien,

        many thanks. That helped me. I had trouble with my localDB instance which was not correct configured thus the emulator didn’t start. After re-installing all required tools (mentioned in your link) everything is working as expected.


  3. How might one create a service bus triggered function with a DYNAMIC connection string?

    Let’s say I have 4 different queues and depending on some configuration setting I want my functions to be configured with one or another of those service bus connection strings.

    I’ve looked at binding. Can’t see how that helps.

    I’ve looked at StartUp . Configure, but still, can’t get my Topic Triggered Functions to load correctly.

    Any ideas?

  4. @DaveCline – Unfortunately you can’t as the connection string is required at instantiation time and is a read-only property. You’ll have to create multiple instances.

Leave a Reply

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

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: