How to create Azure service bus queues automatically using c# and Azure service bus management libraries
Hello Readers!!
This article is about providing you with an alternative solution on how to create this queue(s) programmatically using c# with Azure service bus management libraries.
Hope you are aware of this service and the purpose of using it in the project.
Pros with this approach: No need to ask and wait for DevOps to create these queue(s).
Real-Time Scenario(s): There are many reasons we go for this,
Ex:
- On-demand incoming requests to create a queue(s).
- For newer feature implementation.
To achieve this, firstly we need the following libraries to install from Nuget,
Libraries: Using the below two libraries, which allow interaction with the Service bus management tasks, one of the tasks is creating Queue(s)
Azure.Identity;
Azure.Messaging.ServiceBus.Administration;
The following code snippet helps you to achieve this requirement,
While you use this code for testing or integration in your project, please make sure to replace the following,
- Service bus connection string
- Queue name which you want to create.
The ServiceBusAdministrationClient class from Azure.Messaging.ServiceBus.Administration namespace is used to interact with the Azure Service Bus namespace. The CreateQueueAsync method creates a new queue with the specified name. The method returns a QueueProperties object representing the newly created queue.
After creating the queue, the example prints a success message with the name of the created queue.
Remember to handle exceptions appropriately and ensure you have the necessary permissions to create queues in your Azure Service Bus namespace.
Note: The code snippets provided are based on the Azure.Messaging.ServiceBus.Administration version 7.7.0 library. Ensure you have the correct version installed in your project
Hope it helps :)