Creating an Azure ServiceBus Queue via code

拥有回忆 提交于 2020-07-19 04:56:05

问题


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


回答1:


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.




回答2:


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




回答3:


You can create a Service Bus Queue using NamespaceManager likewise,

QueueDescription _serviceBusQueue = new QueueDescription("QUEUENAME");   //assign the required properties to _serviceBusQueue 

NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString("CONNECTIONSTRING");

var queue = await namespaceManager.CreateQueueAsync(_azureQueue);


来源:https://stackoverflow.com/questions/55650497/creating-an-azure-servicebus-queue-via-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!