Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Azure ServiceBus Queue via code

Apologies, I'm new to Azure. I created a service bus and queue via the Azure portal using this tutorial.

I can write and read from the queue ok. The problem is, to deploy to the next environment, I have to either update the ARM template to add the new queue or create the queue in code. I can't create the queue through the portal in the next environment.

I've chosen the latter: check to see if the queue exists and create as required via code. I already have an implementation for this for a CloudQueueClient (in the Microsoft.WindowsAzure.Storage.Queue namespace). This uses a CloudStorageAccount entity to create the CloudQueueClient, if it doesnt exists.

I was hoping it would be this simple but it appears not. I'm struggling to find a way to create a QueueClint (in the Microsoft.Azure.ServiceBus namespace). All I have is the Service Bus connection string and the queue name but having scoured Microsoft docs, there's talk of a NamespaceManager and MessagingFactory (in a different namespace) involved in the process.

Can anyone point me in the direction of how to achieve this and more importantly, is this the right approach? I'll be using DI to instantiate the queue so the check/creation will only be done once.

The solution is required for a service bus queue and not a storage account queue. Differences outlined here

Thanks

like image 871
iKnowNothing Avatar asked Apr 12 '19 11:04

iKnowNothing


People also ask

How do I get Azure Service Bus connection string?

In order to connect to Azure Service Bus go to Azure portal, open the Service Bus namespace you want, then go to: Settings -> Shared access policies -> RootManageSharedAccessKey -> Primary connection string.

What is the difference between Azure storage queue and Service Bus queue?

Storage queues provide a uniform and consistent programming model across queues, tables, and BLOBs – both for developers and for operations teams. Service Bus queues provide support for local transactions in the context of a single queue.


1 Answers

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.

like image 171
iKnowNothing Avatar answered Sep 30 '22 12:09

iKnowNothing