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?
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
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:
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());
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With