iKn*_*ing 7 c# azure azure-servicebus-queues .net-core asp.net-core-2.0
抱歉,我是 Azure 的新手。我使用本教程通过 Azure 门户创建了一个服务总线和队列。
我可以从队列中读写。问题是,要部署到下一个环境,我必须更新 ARM 模板以添加新队列或在代码中创建队列。我无法在下一个环境中通过门户创建队列。
我选择了后者:检查队列是否存在并通过代码根据需要创建。我已经有一个CloudQueueClient 的实现(在 Microsoft.WindowsAzure.Storage.Queue 命名空间中)。这使用CloudStorageAccount实体来创建 CloudQueueClient(如果它不存在)。
我希望它会这么简单,但似乎不是。我正在努力寻找一种方法来创建QueueClint(在 Microsoft.Azure.ServiceBus 命名空间中)。我所拥有的只是服务总线连接字符串和队列名称,但在搜索了 Microsoft 文档后,有人谈到了流程中涉及的NamespaceManager和MessagingFactory(在不同的命名空间中)。
任何人都可以指出我如何实现这一目标,更重要的是,这是正确的方法吗?我将使用 DI 来实例化队列,因此检查/创建只会执行一次。
服务总线队列需要该解决方案,而不是存储帐户队列。此处概述的差异
谢谢
iKn*_*ing 13
Sean Feldman's answer pointed me in the right direction. The main nuget packages/namespaces required (.net core ) are
Microsoft.Azure.ServiceBus.Management
Here's my solution:
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;
}
Run Code Online (Sandbox Code Playgroud)Not the easiest thing to find but hope it helps someone out.
已接受答案中的 nuget包Microsoft.Azure.ServiceBus现已弃用。要使用该Azure.Messaging.ServiceBus包来代替,您想要的代码如下:
using Azure.Messaging.ServiceBus.Administration;
var client = new ServiceBusAdministrationClient(connectionString);
if (!await client.QueueExistsAsync(queueName))
{
await client.CreateQueueAsync(queueName);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3109 次 |
| 最近记录: |