12

Microsoft has updated their .NET ServiceBus client library, and their documentation is currently split between the old WindowsAzure.ServiceBus package and the new Microsoft.Azure.ServiceBus package. I like the new package, as its a lot cleaner and has less dependencies. In the old package, we had methods like the following:

if (!namespaceManager.TopicExists(topicName))
{
    var topic = new TopicDescription(topicName);
    namespaceManager.CreateTopic(topic);
}

The documentation for creating a topic programmatically still uses the old package, with code like the above. The NamespaceManager class is not available in the new package, so how can I achieve the equivalent of this?

Thomas
  • 24,234
  • 6
  • 81
  • 125
Andrew Williamson
  • 8,299
  • 3
  • 34
  • 62

4 Answers4

16

Update Jan 2022

Microsoft recommends to use ServiceBusAdministrationClient in their latest package Azure.Messaging.ServiceBus.

const string Topic = "<YourTopic>";    

// Create the topic if it doesn't exist
var adminClient = new ServiceBusAdministrationClient(ConnectionString);
if (!await adminClient.TopicExistsAsync(Topic))
    await adminClient.CreateTopicAsync(Topic);

And similar for creating subscriptions.

Thanks to Quan for the update


Original answer

On the Github Repo azure-service-bus-dotnet, they explains how to manage Service Bus entities :

The standard way to manage Azure resources is by using Azure Resource Manager. In order to use functionality that previously existed in the .NET Framework Service Bus client library, you will need to use the Microsoft.Azure.Management.ServiceBus library. This will enable use cases that dynamically create/read/update/delete resources.

There is a sample on how to use this library:

you need to install these packages:

  • Microsoft.Azure.Management.ServiceBus
  • Microsoft.Azure.Management.ResourceManager
  • Microsoft.IdentityModel.Clients.ActiveDirectory

The interesting part for you if you want to create a topic. Note that you don't need to check if the topic exists. Azure resource manager only updates the resource if it already exists.

// On you've got the ServiceBusManagementClient
ServiceBusManagementClient sbClient = ...

sbClient.Topics.CreateOrUpdateAsync("resource group name", "namespace name", "topic name", 
    new Microsoft.Azure.Management.ServiceBus.Models.SBTopic());
Andrew Williamson
  • 8,299
  • 3
  • 34
  • 62
Thomas
  • 24,234
  • 6
  • 81
  • 125
  • Thanks Thomas. I skimmed over the readme and didn't pick up that "manage service bus entities" equates to "create/read/update topics and subscriptions". What's the deal with the "resource group name"? I assume this is the same as resource groups I see in the Azure portal, but doesn't that make code like this a bit easier to break if the resources are shifted around in Azure? – Andrew Williamson Dec 11 '17 at 22:45
  • Yes it is the name of the resource group on Azure. I guess it depends if you are using multiple resource groups. Maybe you can ask another question to deal with it. – Thomas Dec 11 '17 at 22:49
9

In .NET Core you can use ManagementClient to do so, which is easier comparing to namespace manager.

try
{   
    await managementClient.GetTopicAsync(topicName);
}
catch (MessagingEntityNotFoundException)
{   
    await managementClient.CreateTopicAsync(new TopicDescription(topicName) { EnablePartitioning = true });
}

try
{
    await managementClient.GetQueueAsync(queueName);
}
catch (MessagingEntityNotFoundException)
{
    await managementClient.CreateQueueAsync(new QueueDescription(queueName) { EnablePartitioning = true });
}

See azure-service-bus-dotnet/issues/65

HojjatK
  • 1,858
  • 1
  • 17
  • 16
6

The accepted answer was in 2017 so there is an update on how we create a topic on Azure Service Bus as today 1/13/2022.

Microsoft recommends to use ServiceBusAdministrationClient in their latest package Azure.Messaging.ServiceBus.

Install NuGet package: Azure.Messaging.ServiceBus

And here is how you will create a new topic:

const string Topic = "<YourTopic>";    

// Create the topic if it doesn't exist
var adminClient = new ServiceBusAdministrationClient(ConnectionString);
if (!await adminClient.TopicExistsAsync(Topic))
    await adminClient.CreateTopicAsync(Topic);

And similar for creating subscriptions.

This answer is based on this article https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-management-libraries#manage-using-service-bus-client-libraries

Quan
  • 473
  • 7
  • 16
  • 1
    Hi Quan, thanks for the update. I can't accept two answers on one question, so I'm going to add the contents of this as an update to Thomas' answer – Andrew Williamson Jan 13 '22 at 21:51
1

There's also a future option if you can wait - NamespaceManager as a standalone package described in the following issue. The options it will support are listed in the issue as well.

  • Get - limited to exists check and give metadata back
  • GetRuntimeInformation (Gets all counts and last state, Approximate count, accurate within 10s)
  • GetQueueNames, GetTopicNames (List entity names)
  • Create entity
  • Delete entity
  • Update (Need details on what metadata to update, can be done during implementation)
  • FindOrCreate (Upsert - queue doesn't exists create it)
  • UpdateOrCreate (Upsert)

If you'd like the ease of NamespaceManager, then worth following the issue.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80