如何根据函数触发器中的 属性 绑定 azure 函数输入?

How can I bind an azure function input, based on a property in the function trigger?

我想创建一个由事件中心消息触发的 Azure 函数。我还想使用 DocumentDb 中的文档,从触发消息(事件中心消息)的内容中获取 DocumentId。 我不明白这是怎么可能的,我怀疑它是否是,但我想试一试。 在输入中,我选择了 DocumentDB 并在 DocumentId 输入框中(默认为 {documentId},我输入了 {myEventHubMessage.DocumentId},其中 myEventHubMessage 是我的触发器名称,DocumentId json 属性在邮件内容中。

知道这是否可行以及我如何解决这个问题(无需在我的函数中硬编码 DocDb 连接字符串)

是的,这是可能的。下面是一个 C# 示例,首先显示代码,然后显示绑定元数据。对于像 Node 这样的其他语言,绑定元数据将是相同的,只是代码不同。 DocumentDB 绑定通过绑定表达式 {DocId}.

绑定到传入消息的 DocId 属性

代码如下:

#r "Microsoft.ServiceBus"

using System;
using Microsoft.ServiceBus.Messaging;

public static void Run(MyEvent evt, MyDocument document, TraceWriter log)
{
    log.Info($"C# Event Hub trigger function processed event: {evt.Id}");
    log.Info($"Document {document.Id} loaded. Value {document.Value}");
}

public class MyEvent
{
    public string Id { get; set; }
    public string DocId { get; set; }
}

public class MyDocument
{
    public string Id { get; set; }
    public string Value { get; set; }
}

以及绑定元数据:

{
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "evt",
      "direction": "in",
      "path": "testhub",
      "connection": "<your connection>"
    },
    {
      "type": "documentdb",
      "name": "document",
      "databaseName": "<your database>",
      "collectionName": "<your collection>",
      "id": "{DocId}",
      "connection": "<your connection>",
      "direction": "in"
    }
  ]
}