这是我的function.json:

{
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "webHookType": "genericJson",
      "name": "req"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "documentDB",
      "name": "inputDocument",
      "databaseName": "MyDb",
      "collectionName": "MyCol",
      "partitionKey": "main",
      "id": "{documentId}",
      "connection": "MyDocDbConnStr",
      "direction": "in"
    }
  ],
  "disabled": false
}


这是我的run.csx:

#r "Newtonsoft.Json"

using System;
using System.Net;
using Newtonsoft.Json;

public static async Task<object> Run(HttpRequestMessage req, TraceWriter log, dynamic inputDocument)
{

    return req.CreateResponse(HttpStatusCode.OK, $"doc title is {inputDocument.title}");
}


如果我在config中为我的文档ID定义了一个固定值,则一切正常。

但是,当我想使用动态文档ID并使用{documentId}时,出现此错误:

No binding parameter exists for 'documentId'.


我的帖子数据是:

{
    "documentId": "002"
}


如何将DocumentId发送到我的Azure函数并从DocumentDb获取关联项?

最佳答案

要在C#绑定表达式中使用自定义参数,必须在触发器输入绑定到的类型上定义这些属性。由于要从输入有效内容绑定到documentId,因此我们定义了具有相应Input属性的DocumentId POCO。这是一个工作示例:

#r "Newtonsoft.Json"

using System;
using System.Net;
using Newtonsoft.Json;

public class Input
{
    public string DocumentId { get; set; }
}

public static HttpResponseMessage Run(Input input,
              HttpRequestMessage req, dynamic document, TraceWriter log)
{
    if (document != null)
    {
        log.Info($"DocumentId: {document.text}");
        return req.CreateResponse(HttpStatusCode.OK);
    }
    else
    {
        return req.CreateResponse(HttpStatusCode.NotFound);
    }
}


这是对应的function.json:

{
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "webHookType": "genericJson",
      "name": "input"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "documentDB",
      "name": "document",
      "databaseName": "ItemDb",
      "collectionName": "ItemCollection",
      "id": "{documentId}",
      "connection": "test_DOCUMENTDB",
      "direction": "in"
    }
  ]
}

关于c# - 使用动态DocumentId从Azure函数访问DocumentDb,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42829465/

10-10 03:00