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

查看:28
本文介绍了通过代码创建 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 文档之后,有人谈到了 NamespaceManagerMessagingFactory(在不同的命名空间中)参与该过程.

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 核心)是

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>异步客户端;私有只读 QueueClient 客户端;

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天全站免登陆