Azure 函数服务总线触发器:如何停止事件流中的批量数据并且一次只允许队列中的一条消息?

Azure function service bus trigger: How to stop batches of data from event stream and only allow one message from the queue at a time?

这是我第一次使用Azure函数和服务总线,所以请原谅我的无知。

我正在尝试在 visual studio 中构建一个函数应用程序,并且我能够连接到队列主题(我无法控制)。问题是我每次打开断点,在VS中一次处理10s的消息,这使得本地测试非常困难(更不用说数据库池产生的问题)。

如何确保一次只处理 1 条消息,直到我点击完成?

public static void Run([ServiceBusTrigger("xxx", "yyy", AccessRights.Manage)]BrokeredMessage msg, TraceWriter log)
{
     // do something here for one message at a time.
}

在 host.json 中将 maxConcurrentCalls 设置为 1。你也可以从 host.json reference for Azure Functions

得到答案

maxConcurrentCalls 16 The maximum number of concurrent calls to the callback that the message pump should initiate. By default, the Functions runtime processes multiple messages concurrently. To direct the runtime to process only a single queue or topic message at a time, set maxConcurrentCalls to 1

以防将来有人偶然发现这个问题,对于函数应用程序 2.0,您需要像这样更新 host.json

{
  "version": "2.0",
  ...
  "extensions": {
    "serviceBus": {
      "messageHandlerOptions": {
        "maxConcurrentCalls": 1
      }
    }
  }
}

由于 host.json 文件通常与函数本身一起发布到 Azure,最好不要出于调试和开发目的修改它。对 host.json 的任何更改都可以改为 local.settings.json

以下是将 maxConcurrentCalls 设置为 1 的方法:

{
  "IsEncrypted": false,
  "Values": {
    ...
    "AzureFunctionsJobHost:Extensions:ServiceBus:MessageHandlerOptions:MaxConcurrentCalls": 1
  }
}

此处描述了此覆盖功能:https://docs.microsoft.com/en-us/azure/azure-functions/functions-host-json#override-hostjson-values