首页 > 解决方案 > 用于访问字典值的 Azure 函数绑定表达式

问题描述

我正在尝试在 function.json 文件中创建绑定,该文件将根据触发函数的服务总线消息启用 Cosmos DB 文档的输入。

我想在服务总线消息的 UserProperties 字典中使用一个值。文档告诉我,这可用于绑定中:https ://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus#trigger---message-metadata

但是,当我收到以下错误时,我似乎无法在字典中使用值: Microsoft.Azure.WebJobs.Host: Invalid template '{UserProperties['MyId']}'. Invalid template expression 'UserProperties['MyId'].

我的 function.json 的内容是:

{
  "scriptFile": "__init__.py",
  "disabled": false,
  "bindings": [
    {
      "name": "msg",
      "type": "serviceBusTrigger",
      "direction": "in",
      "topicName": "mytopic",
      "subscriptionName": "mysubscription",
      "connection": "myservicebus_SERVICEBUS"
    },
    {
      "name": "documents",
      "type": "cosmosDB",
      "databaseName": "Cache",
      "collectionName": "ProductVersion",
      "id" : "{UserProperties['MyId']}",
      "partitionKey": "{UserProperties['MyId']}",
      "connectionStringSetting": "mycosmos_COSMOSDB",     
      "direction": "in"
    }
  ]
}

https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings#binding-expressions-and-patterns上的文档说我可以使用点符号来访问对象的属性,但没有提到访问字典值。

我也尝试过双引号"partitionKey": "{UserProperties[\"MyId\"]}"和点符号"partitionKey": "{UserProperties.MyId}",但这些也不起作用。

该函数恰好是用 Python 编写的,但希望这无关紧要。

我做错了什么,还是不支持?

更新 1 我使用时遇到的错误{UserProperties.MyId}是:

Exception: TypeError: unable to decode incoming TypedData: unsupported combination of TypedData field None and expected binding type serviceBusTrigger
Stack:   File "d:\Workspace\MyFunction\.env\lib\site-packages\azure\functions_worker\dispatcher.py", line 250, in _handle__invocation_request
    pytype=pb_type_info.pytype)
  File "d:\Workspace\MyFunction\.env\lib\site-packages\azure\functions_worker\bindings\meta.py", line 291, in from_incoming_proto
    f'unable to decode incoming TypedData: '

所以也许这是 Python 特有的问题?

Update2 无论属性是否为字典,都会发生此错误。main定义如下所示:

def main(msg: func.ServiceBusMessage, documents: func.DocumentList):

更新 3 当服务总线消息有效负载为空时会发生错误。该问题已报告:https ://github.com/Azure/azure-functions-python-worker/issues/330

标签: azure-functions

解决方案


推荐阅读