Creating an Azure ServiceBus Queue via code

Sean Feldman's answer pointed me in the right direction. The main nuget packages/namespaces required (.net core ) are

  • Microsoft.Azure.ServiceBus
  • Microsoft.Azure.ServiceBus.Management

Here's my solution:

private readonly Lazy<Task<QueueClient>> asyncClient;
private readonly QueueClient client;`
  
public MessageBusService(string connectionString, string queueName)
{
    asyncClient = new Lazy<Task<QueueClient>>(async () =>
    {
        var managementClient = new ManagementClient(connectionString);

        var allQueues = await managementClient.GetQueuesAsync();

        var foundQueue = allQueues.Where(q => q.Path == queueName.ToLower()).SingleOrDefault();

        if (foundQueue == null)
        {
            await managementClient.CreateQueueAsync(queueName);//add queue desciption properties
        }


        return new QueueClient(connectionString, queueName);
    });

    client = asyncClient.Value.Result; 
}

Not the easiest thing to find but hope it helps someone out.


To create entities with the new client Microsoft.Azure.ServiceBus you will need to use ManagemnetClient by creating an instance and invoking CreateQueueAsync().