Sergio Magrì
Sergio Magrì

Reputation: 13

Azure service bus: how to get topics

I'm totally new with Azure Service Bus, and just for studing I'm trying to get topics. If I'm not wrong I should use class ServiceBusAdministrationClient but when I tried to achieve my goal I got the error "InvalidSignature: The token has an invalid signature".

I'm using the easiest constructor (https://learn.microsoft.com/en-us/dotnet/api/azure.messaging.servicebus.administration.servicebusadministrationclient.-ctor?view=azure-dotnet#azure-messaging-servicebus-administration-servicebusadministrationclient-ctor(system-string) ) passing the same connectionString that allows me to send and receive messages using ServiceBusClient.

Theorically I should be able to do that using the same connection string, I think that because the Service Bus Explorer developed by Paolo Salvatori (https://github.com/paolosalvatori/ServiceBusExplorer) is able to do that with the same connection string. Am I wrong or do I need a different connection string?

Here the code I have (copied from another post):

var administrationClient = new ServiceBusAdministrationClient(ConnectionString);
AsyncPageable<TopicProperties> allTopics = administrationClient.GetTopicsAsync();
await foreach (TopicProperties topic in allTopics)
{
    Console.WriteLine($"IterateTopicsWithAwaitForeachAsync: {topic.Name}");
}

Can anyone please explain me what I'm doing wrong?

ps. I'm using .net 8

Upvotes: 1

Views: 324

Answers (2)

RithwikBojja
RithwikBojja

Reputation: 11411

Thanks @Sean Feldman and I do agree that there might be a chance of Traffic being blocked otherwise if you give correct connection string, it will work and I have taken connection string from below:

enter image description here

The topics i have are:

enter image description here

With Azure.Messaging.ServiceBus.Administration package(ServiceBusAdministrationClient) :

using Azure.Messaging.ServiceBus.Administration;

string ri_cs = "Endpoint=sb://rithtest.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=6YXOZY=";
var ri_cl = new ServiceBusAdministrationClient(ri_cs);
var ri_tp = ri_cl.GetTopicsAsync();
await foreach (var ri in ri_tp)
{
    Console.WriteLine($"Hello Rithwik Bojja, Topic Name: {ri.Name}");
}

Output:

enter image description here

Alterantively you can use Microsoft.Azure.ServiceBus.Management package(ManagementClient) :

using Microsoft.Azure.ServiceBus.Management;

string ri_cs = "Endpoint=sb://rithtest.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=6YXOhY=";
var ri_cl = new ManagementClient(ri_cs);
var rith = await ri_cl.GetTopicsAsync();
foreach (var ri in rith)
{
    Console.WriteLine(ri.Path);
}

Output:

enter image description here

Upvotes: 2

Sean Feldman
Sean Feldman

Reputation: 26057

var administrationClient = new ServiceBusAdministrationClient(ConnectionString);
AsyncPageable<TopicProperties> allTopics = administrationClient.GetTopicsAsync();
await foreach (TopicProperties topic in allTopics)
{
    Console.WriteLine($"IterateTopicsWithAwaitForeachAsync: {topic.Name}");
}

This code is working fine. I'd double-check your connection string. Also, it is worth noting that the administrative client works via HTTP while the data client works via AMQP by default. So, there might be a chance that your data operations are working while management operations fail if HTTP traffic is blocked.

Upvotes: 1

Related Questions