gpt4 book ai didi

c# - 通过代码创建Azure ServiceBus队列

转载 作者:行者123 更新时间:2023-12-03 17:50:21 25 4
gpt4 key购买 nike

抱歉,我是 Azure 新手。我使用此 tutorial 通过 Azure 门户创建了服务总线和队列.

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

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

我希望事情会这么简单,但事实似乎并非如此。我正在努力寻找一种方法来创建 QueueClint (在 Microsoft.Azure.ServiceBus 命名空间中)。我所拥有的只是服务总线连接字符串和队列名称,但在浏览了 Microsoft 文档后,有人谈到 NamespaceManagerMessagingFactory (在不同的命名空间中)参与该过程。

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

服务总线队列需要该解决方案,而不是存储帐户队列。差异概述here

谢谢

最佳答案

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

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

这是我的解决方案:

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;
}

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

关于c# - 通过代码创建Azure ServiceBus队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55650497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com