服务总线未在 Azure 上触发,在本地工作

Service bus not triggering on Azure, working locally

当托管在 Azure 上时,我无法触发我的 Azure 函数,使用 Azure 服务总线队列作为触发器。它在本地 运行 时工作正常,但在远程不工作。

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "msg",
      "type": "serviceBusTrigger",
      "direction": "in",
      "AzureWebJobsStorage": "AzureWebJobsStorage",
      "queueName": "selectivitycalculation",
      "connection":  "AzureServiceBusConnectionString"
    }
  ]
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsStorage": XXXXXX,
    "AzureServiceBusConnectionString": XXXXXXX
  }
}

host.json

{
    "version": "2.0",
    "extensions": {
        "serviceBus": {
            "prefetchCount": 100,
            "messageHandlerOptions": {
                    "autoComplete": false,
                    "maxConcurrentCalls": 32,
                    "maxAutoRenewDuration": "00:55:00"
            }
        }
    },
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"
    },
    "functions": [ "SelectivityCalculation" ],
    "logging": {
        "fileLoggingMode": "debugOnly",
        "logLevel": {
          "Function.MyFunction": "Information",
          "default": "None"
        },
        "applicationInsights": {
            "samplingSettings": {
              "isEnabled": true,
              "maxTelemetryItemsPerSecond" : 20
            }
        }
    }
}

__init__.py

import azure.functions as func
import logging
import sys
from .azure_functions_controller import selectivity_callback

def main(msg: func.ServiceBusMessage):
    logger.info('Python ServiceBus queue trigger processed message.')
    print('working')
    selectivity_callback(msg.get_body())

文件夹结构

Calculator
 | - __app__
 | | - SelectivityCalculation
 | | | - __init__.py
 | | | - azure_functions_controller.py
 | | | - functions.json
 | | - SharedCode
 | | | - ...
 | | - host.json
 | | - bin

以及我在 Azure 上的应用程序设置

我的开发环境如下:

1) Windows 10 专业版 2) VS 代码 1.41.1 3) Azure 函数 CLI 2.7.1948 4) Python 3.7.4

您还记得将功能设置文件上传到 Azure 吗?

至少您需要这些设置才能使函数 运行 正常:

[
  {
    "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
    "value": "xxxxxxxxxxxxxxxx",
    "slotSetting": false
  },
  {
    "name": "AzureWebJobsStorage",
    "value": "xxxxxxxxxxxxxxxx",
    "slotSetting": false
  },
  {
    "name": "bowmantest_SERVICEBUS",
    "value": "xxxxxxxxxxxxxxxx",
    "slotSetting": false
  },
  {
    "name": "BUILD_FLAGS",
    "value": "UseExpressBuild",
    "slotSetting": false
  },
  {
    "name": "ENABLE_ORYX_BUILD",
    "value": "true",
    "slotSetting": false
  },
  {
    "name": "FUNCTIONS_EXTENSION_VERSION",
    "value": "~2",
    "slotSetting": false
  },
  {
    "name": "FUNCTIONS_WORKER_RUNTIME",
    "value": "python",
    "slotSetting": false
  },
  {
    "name": "SCM_DO_BUILD_DURING_DEPLOYMENT",
    "value": "1",
    "slotSetting": false
  },
  {
    "name": "XDG_CACHE_HOME",
    "value": "/tmp/.cache",
    "slotSetting": false
  }
]

鉴于你在本地运行良好,我怀疑是因为你没有上传配置文件。如果我的Application Settings只有你截图中的三个设置,那么我的Function在触发时不会显示任何内容。

您的配置问题是:

"autoComplete": false,

autocomplete=false is only supported for C#,因此在 Python 中,不可能 以死信的形式交互发送消息。

换句话说,这意味着使消息成为死信的唯一方法是在处理消息时引发异常。