通过代码创建 Azure ServiceBus 队列
Creating an Azure ServiceBus Queue via code
抱歉,我是 Azure 的新手。我使用此 tutorial.
通过 Azure 门户创建了服务总线和队列
我可以写入和读取队列。问题是,要部署到下一个环境,我必须更新 ARM 模板以添加新队列或在代码中创建队列。下个环境无法通过入口创建队列
我选择了后者:检查队列是否存在并根据需要通过代码创建。我已经为 CloudQueueClient (in the Microsoft.WindowsAzure.Storage.Queue namespace). This uses a CloudStorageAccount 实体创建了 CloudQueueClient 的实现,如果它不存在的话。
我希望它会这么简单,但事实并非如此。我正在努力寻找一种方法来创建参与该过程的 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(在不同的命名空间中)。
任何人都可以指出如何实现这一目标的方向,更重要的是,这是正确的方法吗?我将使用 DI 来实例化队列,因此 check/creation 只会执行一次。
服务总线队列而不是存储帐户队列需要该解决方案。差异概述 here
谢谢
要使用新客户端 Microsoft.Azure.ServiceBus 创建实体,您需要使用 ManagemnetClient
by creating an instance and invoking CreateQueueAsync()
。
您可以同样使用 NamespaceManager 创建服务总线队列,
QueueDescription _serviceBusQueue = new QueueDescription("QUEUENAME"); //assign the required properties to _serviceBusQueue
NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString("CONNECTIONSTRING");
var queue = await namespaceManager.CreateQueueAsync(_azureQueue);
Sean Feldman 的回答为我指明了正确的方向。所需的主要 nuget packages/namespaces (.net core) 是
- Microsoft.Azure.ServiceBus
Microsoft.Azure.ServiceBus.管理
这是我的解决方案:
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;
}
不是最容易找到的东西,但希望它能帮助别人。
抱歉,我是 Azure 的新手。我使用此 tutorial.
通过 Azure 门户创建了服务总线和队列我可以写入和读取队列。问题是,要部署到下一个环境,我必须更新 ARM 模板以添加新队列或在代码中创建队列。下个环境无法通过入口创建队列
我选择了后者:检查队列是否存在并根据需要通过代码创建。我已经为 CloudQueueClient (in the Microsoft.WindowsAzure.Storage.Queue namespace). This uses a CloudStorageAccount 实体创建了 CloudQueueClient 的实现,如果它不存在的话。
我希望它会这么简单,但事实并非如此。我正在努力寻找一种方法来创建参与该过程的 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(在不同的命名空间中)。
任何人都可以指出如何实现这一目标的方向,更重要的是,这是正确的方法吗?我将使用 DI 来实例化队列,因此 check/creation 只会执行一次。
服务总线队列而不是存储帐户队列需要该解决方案。差异概述 here
谢谢
要使用新客户端 Microsoft.Azure.ServiceBus 创建实体,您需要使用 ManagemnetClient
by creating an instance and invoking CreateQueueAsync()
。
您可以同样使用 NamespaceManager 创建服务总线队列,
QueueDescription _serviceBusQueue = new QueueDescription("QUEUENAME"); //assign the required properties to _serviceBusQueue
NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString("CONNECTIONSTRING");
var queue = await namespaceManager.CreateQueueAsync(_azureQueue);
Sean Feldman 的回答为我指明了正确的方向。所需的主要 nuget packages/namespaces (.net core) 是
- Microsoft.Azure.ServiceBus
Microsoft.Azure.ServiceBus.管理
这是我的解决方案:
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; }
不是最容易找到的东西,但希望它能帮助别人。