通过代码创建Azure ServiceBus队列 [英] Creating an Azure ServiceBus Queue via code

查看:201
本文介绍了通过代码创建Azure ServiceBus队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,我是Azure的新手.我使用教程.

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

我可以从队列中读写,确定.问题是,要部署到下一个环境,我必须更新ARM模板以添加新队列或在代码中创建队列.我无法在下一个环境中通过门户创建队列.

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.

我选择了后者:检查队列是否存在,并通过代码根据需要创建.我已经有一个 CloudQueueClient (在Microsoft.WindowsAzure.Storage.Queue命名空间中).这使用 CloudStorageAccount 创建CloudQueueClient的实体(如果不存在).

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.

我希望它会如此简单,但事实并非如此.我正在努力寻找一种创建 QueueClint (在Microsoft.Azure.ServiceBus命名空间中).我所拥有的只是服务总线连接字符串和队列名称,但是在搜索Microsoft文档时,谈论的是 MessagingFactory (在另一个名称空间中).

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.

任何人都可以指出我如何实现这一目标的方向,更重要的是,这是正确的方法吗?我将使用DI实例化队列,因此检查/创建将只完成一次.

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

谢谢

推荐答案

Sean Feldman的回答为我指明了正确的方向.所需的主要nuget程序包/命名空间(.net core)是

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

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

这是我的解决方案:

private readonly Lazy<Task<QueueClient>> asyncClient; private readonly QueueClient client;

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.

这篇关于通过代码创建Azure ServiceBus队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆