如何将DocumentDB输入绑定到Azure功能?

时间:2017-05-12 00:10:07

标签: azure azure-functions

我有一个Azure功能,我在Azure门户中创建,现在想要使用与VS2017预览相关的visual studio azure工具重新创建它。

我的功能是定时器触发,还有Azure DocumentDB的输入绑定(带有查询)和绑定到Azure Service Bus队列的输出。

以下是门户网站的functions.json定义:

{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    },
    {
      "type": "serviceBus",
      "name": "outputQueue",
      "queueName": "test-output-requests",
      "connection": "send-refresh-request",
      "accessRights_": "Send",
      "direction": "out"
    },
    {
      "type": "documentDB",
      "name": "incomingDocuments",
      "databaseName": "test-db-dev",
      "collectionName": "TestingCollection",
      "sqlQuery": "select * from c where c.docType = \"Test\"",
      "connection": "my-testing_DOCUMENTDB",
      "direction": "in"
    }
  ],
  "disabled": false
}

在VS2017中,我使用TimerTriggered模板创建Azure Function项目,然后创建Azure函数:

public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, TraceWriter log, ICollector<dynamic> outputQueue, IEnumerable<dynamic> incomingDocuments)
{
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");   

    //Inspect incomingDocuments .. push messages to outputQueue
}

在本地运行,计时器按预期触发 - 但如何在代码中重新创建输入和输出绑定?我不确定我应该使用什么属性,以及我需要在json配置文件中连接它。

1 个答案:

答案 0 :(得分:4)

  1. 添加对以下nuget pacakge的引用 https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.DocumentDB/
  2. 添加using Microsoft.Azure.WebJobs
  3. 更新documentDb参数,如下所示(也添加其他属性)

    [DocumentDB(ConnectionStringSetting = "")]IEnumerable<dynamic> incomingDocuments
    
  4. 更新serviceBus参数,如下所示

    [ServiceBus("test-output-requests",Connection = "ConnectionValue")]
    
  5. 验证bin/functionName/function.json

    中生成的function.json

    谢谢,
    那仁