Create ServiceBus topic if it doesn't already exist
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
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 :
- Can I manage Service Bus entities with this library?:
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:
- Service Bus Management Library Sample
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());
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.
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://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-management-libraries#manage-using-service-bus-client-libraries