c# - 使用Azure功能处理Azure服务总线队列消息

标签 c# azure azure-functions azureservicebus

因此,我们希望在应用程序中卸载一些处理,以提供更好的用户体验,同时仍然完成这些繁重的任务,并找到了使用 Azure 服务总线队列的方法。

我了解如何将数据推送到队列以及消息队列背后的基本思想,但我很难理解的是当它们进入时如何处理它们。只要想想它,听起来应该有某种方法实现和 Azure 函数,每当有消息传入时都会进行监听,但如何在不不断轮询的情况下做到这一点?我知道您可以使用 OnMessage 订阅队列,但是它如何与 Azure 函数一起使用?

例如目前我们正在做这样的事情,

var client = QueueClient.CreateFromConnectionString(connectionString, queueName);

BrokeredMessage message = new BrokeredMessage();

while ((message = client.Receive(new TimeSpan(hours: 0, minutes: 0, seconds: 30))) != null)
{
    Console.WriteLine(string.Format("Message received: {0}, {1}, {2}", message.SequenceNumber, message.Label, message.MessageId));
    message.Complete();

    Console.WriteLine("Processing message (sleeping...)");
    Thread.Sleep(1000);
}

Console.WriteLine("Finished listening Press ENTER to exit program");
Console.ReadLine();

但是在这种情况下我们只是模拟轮询,对吗?这感觉不是一个好的解决方案。我在设计中是否考虑到了这个错误?

最佳答案

Azure ServiceBus 的工作原理是向连接的客户端推送新消息,而不是让客户端轮询队列。

通过 ServiceBus API,您可以使用 OnMessage 方法来设置消息泵,但如果您使用的是 Azure Functions,那么这一切都可以通过使用服务总线触发器来完成.

您只需将 Azure Function 配置为指向您要监听的队列即可。当新消息添加到队列时,您的函数将被触发,并且消息将被传递到其中。

看一下服务总线触发器示例:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus#trigger-sample

关于c# - 使用Azure功能处理Azure服务总线队列消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46775895/

相关文章:

c# - 无法使用 'where' 类型约束转换泛型参数?

C# 在运行时获取定义字典的类型

azure - Windows Azure 和 WCF 数据服务 V3

azure - 使用自定义 nuget.config 的 webapp 的 nuget 包恢复失败

php - 如何在 Azure 上启用 Zend Guard Loader 扩展 (ZendLoader.dll)?

c# - 无逻辑类 : Code Smell or Ugly System Fact?

python-3.x - 使用 Python 上传 Azure Cloud Functions HTTP 文件

c# - 如何使用 C# 脚本 (csx) 从函数内获取 Azure 函数名称

powershell - 在VSCode中错误调试PowerShell Azure函数

c# - 单击组合框按钮的任何特定事件