首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >通过代码创建Azure ServiceBus队列

通过代码创建Azure ServiceBus队列
EN

Stack Overflow用户
提问于 2019-04-12 11:20:29
回答 5查看 5.1K关注 0票数 12

抱歉,我对Azure不熟悉。我使用这个教程通过Azure门户创建了一个服务总线和队列。

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

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

我希望事情会这么简单,但看起来并非如此。我很难找到一种方法来创建一个QueueClint (在Microsoft.Azure.ServiceBus名称空间中)。我所拥有的只是服务总线连接字符串和队列名称,但是在浏览了Microsoft之后,有关于进程中涉及NamespaceManagerMessagingFactory (位于不同名称空间中)的讨论。

谁能指点我如何实现这一目标,更重要的是,这是正确的做法吗?我将使用DI实例化队列,因此检查/创建只能执行一次。

对于服务总线队列,而不是存储帐户队列,需要解决方案。这里概述的差异

谢谢

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2019-04-16 14:47:54

肖恩·费尔德曼的回答为我指明了正确的方向。所需的主要nuget包/命名空间(.net核心)是

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

这是我的解决方案:

代码语言:javascript
代码运行次数:0
运行
复制
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; 
}

不是最容易找到的东西,但希望它能帮到别人。

票数 21
EN

Stack Overflow用户

发布于 2019-04-13 01:09:19

要使用新的客户端Microsoft.Azure.ServiceBus创建实体,需要通过创建实例和调用CreateQueueAsync()来使用ManagemnetClient

票数 5
EN

Stack Overflow用户

发布于 2022-10-19 16:49:40

接受的答案中的Microsoft.Azure.ServiceBus nuget包现在被废弃了。要使用Azure.Messaging.ServiceBus包,您想要的代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
using Azure.Messaging.ServiceBus.Administration;

var client = new ServiceBusAdministrationClient(connectionString);

if (!await client.QueueExistsAsync(queueName))
{
    await client.CreateQueueAsync(queueName);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55650497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档