Search code examples
azureservicebus

Find list of Subscriptions from azure Topic


Is it possible to find a list of Subscriptions on a Service Bus Topic?

I want to be able to find the list and then loop through it.


Solution

  • If you're using C#, you can do something like the following:

    private void EnumerateTopics()
    {
        var namespaceManager = NamespaceManager.CreateFromConnectionString(c_ConnectionString);
        const string topicName = "testtopic";
    
        var subscriptions = namespaceManager.GetSubscriptions(topicName);
    
        // do stuff with subscriptions
    }
    

    That's obviously all synchronous, but there are corresponding async versions of the calls (GetSubscriptionsAsync, for example). The subscriptions object is an IEnumerable<SubscriptionDescription>, which'll let you get at any other aspect of the subscription that you might want to use.

    First you'll need to:

    1. add the corresponding "Windows Azure Service Bus" NuGet package to your solution
    2. add a reference to "Microsoft.ServiceBus" to the corresponding csproj
    3. and obviously drop the using statement using Microsoft.ServiceBus; in your .cs file

    Your connection string will be exactly what you pull from the Azure Management Portal, roughly along the lines of: Endpoint=sb://[namespacename].servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue=[key]

    There are similarly easy-to-use libraries available for other languages too.