Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create ServiceBus topic if it doesn't already exist

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?

like image 418
Andrew Williamson Avatar asked Dec 11 '17 21:12

Andrew Williamson


3 Answers

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());
like image 155
Thomas Avatar answered Nov 01 '22 03:11

Thomas


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

like image 9
HojjatK Avatar answered Nov 01 '22 02:11

HojjatK


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

like image 5
Quan Avatar answered Nov 01 '22 01:11

Quan