2
votes

I am migrating my project from the WindowsAzure.ServiceBus to the new Microsoft.Azure.Servicebus NuGet package.

The problem is that I cannot find a way to list all my current topics/subscriptions in my servicebus namespace.

This is the piece of code I used before with the old NuGet package:

var namespaceManager = NamespaceManager.CreateFromConnectionString("ServiceBusConnectionString");

foreach (var topic in await namespaceManager.GetTopicsAsync())
{
    foreach (var subscription in await namespaceManager.GetSubscriptionsAsync(topic.Path))
    {
        //do something
    }
}
foreach (var queue in await namespaceManager.GetQueuesAsync())
{
    //do something
}

Edit: The latest version has support for listing all topics, subscriptions and queues.

var managementClient = new ManagementClient("ServiceBusConnectionString");

foreach (var topic in await _managementClient.GetTopicsAsync())
{
    foreach (var subscription in await _managementClient.GetSubscriptionsAsync(topic.Path))
    {
        //do something
    }
}
1

1 Answers

2
votes

Microsoft.Azure.Servicebus This is the next generation Service Bus .NET client library that focuses on queues & topics.You could get more information about Microsoft.Azure.Servicebus from github.

If you need management opersations, the new client won't provide it. I recommend that you'd better use Management library or wait till a replacement package for NamespaceManager is out.

If Management library is possible, you use the following demo code to list the subscription .For more details about how to use the Management Library, you could refer to another SO thread.

var credentials = SdkContext.AzureCredentialsFactory.FromFile(@"authpath");
var azure = Azure
            .Configure()
            .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
            .Authenticate(credentials)
            .WithDefaultSubscription();
var sbNameSpace = "service bus subscription";
var resoureGroup = "resourcegroup";
var topicName = "topicName"
var servicebus = azure.ServiceBusNamespaces.GetByResourceGroup(resoureGroup, sbNameSpace);
var topic = servicebus.Topics.GetByName(topicName);
var subscription = topic.Subscriptions.List();