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());