抱歉,我对Azure不熟悉。我使用这个教程通过Azure门户创建了一个服务总线和队列。
我可以从队列中写和读。问题是,要部署到下一个环境,我必须更新ARM模板以添加新队列或在代码中创建队列。我无法在下一个环境中通过门户创建队列。
我选择了后者:检查队列是否存在,并根据需要通过代码创建队列。我已经为CloudQueueClient (在Microsoft.WindowsAzure.Storage.Queue命名空间中)提供了一个实现。这使用CloudStorageAccount实体来创建CloudQueueClient,如果它不存在的话。
我希望事情会这么简单,但看起来并非如此。我很难找到一种方法来创建一个QueueClint (在Microsoft.Azure.ServiceBus名称空间中)。我所拥有的只是服务总线连接字符串和队列名称,但是在浏览了Microsoft之后,有关于进程中涉及NamespaceManager和MessagingFactory (位于不同名称空间中)的讨论。
谁能指点我如何实现这一目标,更重要的是,这是正确的做法吗?我将使用DI实例化队列,因此检查/创建只能执行一次。
对于服务总线队列,而不是存储帐户队列,需要解决方案。这里概述的差异
谢谢
发布于 2019-04-16 06:47:54
肖恩·费尔德曼的回答为我指明了正确的方向。所需的主要nuget包/命名空间(.net核心)是
这是我的解决方案:
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;
}
不是最容易找到的东西,但希望它能帮到别人。
发布于 2019-04-12 17:09:19
要使用新的客户端Microsoft.Azure.ServiceBus创建实体,需要通过创建实例和调用CreateQueueAsync()
来使用ManagemnetClient
。
发布于 2022-10-19 08:49:40
接受的答案中的Microsoft.Azure.ServiceBus
nuget包现在被废弃了。要使用Azure.Messaging.ServiceBus
包,您想要的代码如下:
using Azure.Messaging.ServiceBus.Administration;
var client = new ServiceBusAdministrationClient(connectionString);
if (!await client.QueueExistsAsync(queueName))
{
await client.CreateQueueAsync(queueName);
}
https://stackoverflow.com/questions/55650497
复制相似问题