gpt4 book ai didi

C# Azure 存储队列 - 从队列读取消息的更有效方法

转载 作者:行者123 更新时间:2023-12-03 01:25:00 26 4
gpt4 key购买 nike

我正在使用 NET Core 5.0.0 开发 API Web ASP.NET Core 项目,并使用 Azure.Storage.Queues 12.6.0 来写入和读取队列消息。

一切正常,但我想知道我阅读消息的方式是否合适,或者在速度和效率方面有更好的方法。

这是我正在使用的代码。它只是 Microsoft 教程中的一段代码,放在 while() 循环中。 AzureQueue 只是 QueueClient 类的一个实例。

    public async Task StartReading()
{
while (true)
{
if (await AzureQueue.ExistsAsync())
{
QueueProperties properties = await AzureQueue.GetPropertiesAsync();

if (properties.ApproximateMessagesCount > 0)
{
QueueMessage[] retrievedMessage = await AzureQueue.ReceiveMessagesAsync(1);
string theMessage = retrievedMessage[0].MessageText;
await AzureQueue.DeleteMessageAsync(retrievedMessage[0].MessageId, retrievedMessage[0].PopReceipt);
}
}
}
}

老实说,我不习惯使用无限的 while() 循环,因为在我看来,它是我无法控制和停止的东西。但除了我个人的感受之外,这种 while() 是一种可以接受的方法还是我应该使用其他方法?我只需要在消息到达队列时立即继续读取消息。

最佳答案

您可以做的是将代码放入 Azure 函数中。

然后绑定(bind)azure函数,当队列中有东西时触发。

参见:https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue-trigger?tabs=csharp

关于C# Azure 存储队列 - 从队列读取消息的更有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66783315/

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