Azure Service Bus .NET SDK Deep Dive – Creating queues

Explains how to create queues with the management client, for more posts in this series go to Contents.

Without queues, topics and subscriptions (also called entities) there is not much we can do with an Azure Service Bus namespace. For the start it is sufficient enough to create a queue that we will use to send messages to and receive messages from. Creating entities requires a reference to the Microsoft.Azure.Management.ServiceBus package. In contrast to the older (new legacy Azure Service Bus SDK) the management client has been separated out into its own package to clearly separate runtime operations such as sending and receiving from creating, modifying and deleting entities.

Once we have a reference to the management package a management client can be created to interact with the namespace.

var client = new ManagementClient(connectionString);
if (!await client.QueueExistsAsync("queue")) 
   await client.CreateQueueAsync("queue");
await client.CloseAsync();

The above code demonstrates how to query whether an existing queue exists and if it doesn’t create the queue with name “queue” (I know very creative). Deleting a queue is equally simple:

var client = new ManagementClient(connectionString);
if (await client.QueueExistsAsync("queue")) 
   await client.DeleteQueueAsync("queue");
await client.CloseAsync();

For the management operations to work the access policy used to connect to the broker requires the Manage claim.

The really nice thing about those claims is that if you don’t require to create entities during runtime and have a fairly static set of queues, topics and subscriptions (more about those later) you can have two SAS Policies. One SAS policy with manage rights that is only used to create the necessary entities as part of your deployment scripts and one SAS policy without manage rights that is used to actually send and receive messages on the already constructed entities. That gives you an additional layer of certainty that no-code running in production can actually execute destructive operations such as deleting queues. My friend Damien has a great article about restricted access with Azure Service Bus should you wish to dig deeper.

About the author

Daniel Marbach

2 comments

Recent Posts