【问题标题】:Get access to all queues in Azure Storage Account访问 Azure 存储帐户中的所有队列
【发布时间】:2021-01-07 04:58:01
【问题描述】:

我正在按照 Microsoft 文档中的建议使用 Azure.Storage.Queues 包。我正在使用 Queue Client 连接到队列,如下所示,

public class QueueClientSingleton : IQueueClientSingleton
{
    private readonly Lazy<QueueClient> _queueClientXyz;
    private readonly IConfiguration _configuration;

    public QueueClientSingleton(IConfiguration configuration)
    {
        _configuration = configuration;

        _queueClientXyz = new Lazy<QueueClient>(() => new QueueClient(_configuration.ConnectionString, "QueueXyz");
    }
}

上述代码未经测试,但应该可以提供对 QueueXyz 的访问。我有一个用例,我想通过连接字符串连接到存储帐户一次,然后

  1. 通过不同的队列读取(传递队列名称并从该队列获取消息)
  2. 列出该存储帐户中存在的所有队列

我发现了类似下面的内容,但这是使用已弃用的包:Microsoft.WindowsAzure.Storage,我们没有在我们的解决方案中使用它。有没有我们可以用一个新的包做类似的事情:Azure.Storage.Queues

public static void CreateKnownAzureQueues(string azureConnectionString)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azureConnectionString);
    CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

    foreach (var queueName in AzureQueues.KnownQueues)
    {
        queueClient.GetQueueReference(queueName).CreateIfNotExists();
    }
}

此外,还可以在链接下方找到请求存储帐户中所有队列的列表。但是,它没有提供任何这样的例子。
https://docs.microsoft.com/en-us/rest/api/storageservices/list-queues1#request

【问题讨论】:

    标签: .net azure .net-core azure-storage azure-storage-queues


    【解决方案1】:

    在使用Azure.Storage.Queues包的时候,可以使用下面的代码来达到你的目的:

            QueueServiceClient serviceClient = new QueueServiceClient(conn_str);
            
            //list all queues in the storage account
            var myqueues = serviceClient.GetQueues().AsPages();
    
            //then you can write code to list all the queue names          
            foreach (Azure.Page<QueueItem> queuePage in myqueues)
            {
                foreach (QueueItem q in queuePage.Values)
                {
                    Console.WriteLine(q.Name);
                }
    
            }
    
            //get a queue client
            var myqueue_client = serviceClient.GetQueueClient("the queue name");
    

    【讨论】:

    • 知道如何为多个队列获取队列客户端吗?我知道我们可以使用 serviceClient.GetQueueClient("the queue name") 但这仅适用于一个队列。我们如何同时获得多个队列的队列客户端?基本上我想验证QueueAbc,QueueXyz,QueuePQR,QueueLMN中是否有任何消息?是循环不同队列的唯一选择吗?
    • @Vicky,对于queueClient,只能一个一个去取。
    猜你喜欢
    • 2014-05-02
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 2019-07-28
    • 2022-01-22
    • 2021-01-31
    相关资源
    最近更新 更多